list(列表) python
1.list(列表):
list是处理一组有序项目的数据结构
list(列表)是python中使用最频繁的数据类型
list中什么类型的数据都可以存放(如:类、自己、函数......);
list(列表)可以完成大多数数据集合类的数据结构实现,它支持字符、数字、字符串甚至还可以嵌套列表、元组、字典等
list(列表)用方括号[]标示,内部元素间用逗号隔开。
2. []空列表:
>>> a=[]
>>> a
[]
3. 非空列表:
>>> a=[1,2,3]
>>>
4. 访问列表:元素、索引、切片
可以通过索引访问,与字符串的索引一样,列表索引从0开始
列表可以进行截取、组合等
>>> list1=["physics","chemistry",1997,2000]
>>> list2=[1,2,3,4]
>>> print "list1[0]:",list1[0]
list1[0]: physics
>>> print "list2[1:5]:",list2[1:5]
list2[1:5]: [2, 3, 4]
>>>
4.1. 切片:允许返回空
如:a[len(a):8]
5. list.insert(index,obj)/list.append(obj):列表中插入/添加对象
list.insert(index,obj):index的前一个位置插入obj
list.append(obj):list的最后添加obj
>>> a=[0,1,2,3]
>>> type(a)
<type 'list'>
>>> isinstance(a,list)
True
>>>
>>> a[1]
1
>>> a[-3]
1
>>> a[-1]
3
>>> a[1:-1]
[1, 2]
>>>
>>> a
[1, 1, 2, 2, 3, 3]
>>> a.insert(10,5)
>>> a
[1, 1, 2, 2, 3, 3, 5]
>>>
>>> a
[1, 2, 3]
>>> a.insert(2,5)
>>> a
[1, 2, 5, 3]
>>>
5.1. 练习:统计列表中的列表元素的个数
>>> a=[1,[2],[3],"a"]
>>> c=0
>>> for i in a:
... if isinstance(i,list):
... c+=1
...
>>> print c
2
>>> len(filter(lambda x:isinstance(x,list),a))
2
6. list[index]=obj/append/insert:修改/更新列表
>>> a=[0,"x",1,2,4,6]
>>> a.append(7)
>>> a
[0, 'x', 1, 2, 4, 6, 7]
>>> a.insert(2,"y")
>>> a
[0, 'x', 'y', 1, 2, 4, 6, 7]
>>> a[1]="m"
>>> a
[0, 'm', 'y', 1, 2, 4, 6, 7]
>>>
7. del list.[index]/list.remove(obj):删除列表元素
删除元素:根据索引、元素
删除整个列表
>>> a
[0, 'm', 'y', 1, 2, 4, 6, 7]
>>> del a[2]
>>> a.remove(4)
>>> a
[0, 'm', 1, 2, 6, 7]
>>> a.remove(4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>>
7.1. 删除列表元素:
>>> a=[1,2,4,4,4,4,5,6]
>>> for i in a:
... if i == 4:
... a.remove(i)
...
>>> a
[1, 2, 4, 4, 5, 6]
7.2. 删除列表a中所有的4:
>>> a=[1,2,3,4,4,4,4,4,6]
>>> a.count(4)
5
>>> for i in range(a.count(4)):
... a.remove(4)
...
>>> a
[1, 2, 3, 6]
>>>
>>> filter(lambda x:x!=4,[1,2,3,4,4,4,4,4,6])
[1, 2, 3, 6]
>>>
>>> "".join(map(str,a))
'1236'
>>>
8. + * 列表运算符:
+用于组合列表
*用于重复列表
>>> [1,2]+[5,6]
[1, 2, 5, 6]
>>> ["gloryroad"]*4
['gloryroad', 'gloryroad', 'gloryroad', 'gloryroad']
9. 序列(列表)函数&方法:(以下函数对所有序列都可用,序列:字符串、列表、元组)
9.1. cmp(list1,list2)比较函数
>>> cmp([1],[2])
-1
>>> cmp([3],[2])
1
>>> cmp([2],[2])
0
>>> cmp([],2)
1
>>> cmp([10],[2,2])
1
>>>
9.2. len(list)列表长度函数
9.3. max(list)最大值函数:
>>> max([3],[3,2],[3,3])
[3, 3]
>>> max([1,2,3])
3
9.4. min(list)最小值函数:
>>> min([1,2,3])
1
9.5. list(seq)转换成列表函数
>>> list("abc")
['a', 'b', 'c']
>>> list((1,2,3))
[1, 2, 3]
>>> list({1:"a",2:"b",3:"c"})
[1, 2, 3]
>>>
10. 列表操作函数:(仅对列表有用)
10.1. list.append(obj)
10.2. list.insert(index,obj)
10.3. list.pop(index)移除列表中的一个元素(默认最后一个元素)并且返回该元素的值
>>> a=[1,2,3]
>>> a.pop()
3
>>> a.pop()
2
>>>
>>> {1:2,3:4,5:6}.pop(1)
2
>>> {1:2,3:4,5:6}.pop(3)
4
>>>
10.4. list.remove(obj)移除列表中某个值的第一个匹配项,一次删除一个
10.5. list.index(obj)从列表中找出某个值第一个匹配项的索引位置,如果未找到就会抛出异常
>>> "I am a boy".split().index("boy")
3
>>>strObject.index(“element”):返回第几个单词
10.6. list.count(obj)统计某个元素在列表中出现的次数
10.7. list.extend(seq)在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
>>> a=[1,2,3]
>>> a.extend([4,5,6]) #每个元素当成一个元素
>>> a
[1, 2, 3, 4, 5, 6]
>>> a.append([4,5,6]) #整体当成一个元素
>>> a
[1, 2, 3, 4, 5, 6, [4, 5, 6]]
>>>
10.8. list.reverse()反向列表中元素
>>> a.reverse()
>>> a
[10, 6, 3, 3, 2, -1]
>>> a=["a","b",1,3,"c",4]
>>> a.reverse()
>>> a
[4, 'c', 3, 1, 'b', 'a']
>>>
10.9. list.sort([func])对原列表进行排序,根据ASCII码从大到小进行排序(没有返回值)
>>> a=[3,2,6,3,10,-1]
>>> a.sort()
>>> a
[-1, 2, 3, 3, 6, 10]
>>> print a.sort() #没有返回值
None
10.11. L.sort(cmp=None,key=None,reverse=False)自定义排序:
L.sort(cmp=None,key=None,reverse=False)
对原序列进行排序,直接在原序列上操作,没有返回值;
cmp:为一个定制的比较函数,接受两个参数,并且如果第一个参数小于第二个参数,则返回一个负数(-1),大于则返回一个正数(1),等于则返回0。默认值为None
key:也是一个函数,这个函数会从每个元素中提取一个用于比较的关键字。默认值为None
reverse:接受False或者True,表示是否降序。如果设置为True,表示降序。
例1:
#每个元素中数字之和排序
#coding=utf-8
sentence = "11 22 33 44 394"
word_list=sentence.split()
def L(tup) :
return sum(map(int,list(tup))) #或:reduce(lambda x,y:int(x)+int(y),tup)
word_list.sort(key = L,reverse = True)
print word_list
例2:
#重写sort中的cmp函数
#coding=utf-8
list1 = [(-1,5,3),(-5,3,6,3),(1,1,2,4,5,6),(2,9),(-2,10)]
def compare(a,b):
if abs(a)>abs(b):
return 1
elif abs(a)==abs(b):
return 0
else:
return -1
def L(tup) :
return tup[0]
list1.sort(cmp=compare,key = L,reverse = False)
print list1
list(列表) python的更多相关文章
- 今日、本周、本月时间列表 python方法
def get_time(self): ''' @author : Cong @Email : 624420781@qq.com @描 述 : 生产时间列表 ''' # 生成本日时间列表 i = da ...
- file_list(path):遍历文件列表[python]
import os def __file_list__(path, level): files = os.listdir(path); for i in files: path_tmp = path ...
- python 操作元组 列表===python中三大宝刀(字典已经再上一遍 说过)
字典俗称,世界有多大就能装多大 列表俗称,你们决定 元组俗称,可远观而不可亵玩焉 列表的相关操作a=['1','2','3','5','6','7']# print(a[0])# print(a[0: ...
- Python黑帽编程2.3 字符串、列表、元组、字典和集合
Python黑帽编程2.3 字符串.列表.元组.字典和集合 本节要介绍的是Python里面常用的几种数据结构.通常情况下,声明一个变量只保存一个值是远远不够的,我们需要将一组或多组数据进行存储.查询 ...
- python学习笔记-(四)列表&元组
1.列表及元组操作 1.1 列表 Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 定义列表: >>> namelist = ...
- Python常见数据结构--列表
列表 Python有6个序列的内置类型,但最常见的是列表和元组. 序列都可以进行的操作包括索引,切片.加.乘.检查成员. 此外,Python已经内置确定序列的长度以及确定最大和最下的元素的方法. ...
- python中列表和字典常用方法和函数
Python列表函数&方法 Python包含以下函数: 序号 函数 1 cmp(list1, list2)比较两个列表的元素 2 len(list)列表元素个数 3 max(list)返回列表 ...
- Python学习(9)列表
目录 Python 列表 访问列表中的值 更新列表 删除列表元素 列表脚本操作符 列表截取 列表函数&方法 Python 列表(List) 序列是Python中最基本的数据结构.序列中的每个元 ...
- Python基础教程之第2章 列表和元组
D:\>python Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Typ ...
随机推荐
- MySql-8.0.12 安装教程
MySql-8.0.12 安装教程随笔https://www.cnblogs.com/CrazyDemo/p/9409995.html MySQL 安装https://m.runoob.com/mys ...
- 647. Palindromic Substrings
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- nginx 服务脚本编写模板
编写nginx服务脚本:脚本内容如下: [root@www ~]# cat /etc/init.d/nginx #!/bin/bash # nginx Startup script for the N ...
- Asp.net MVC - 使用PRG模式(附源码)
阅读目录: 一. 传统的Asp.net页面问题 二.Asp.net MVC中也存在同样的问题 三.使用PRG模式 四.PRG模式在MVC上的实现 一. 传统的Asp.net页面问题 一个传统的Asp. ...
- 【Android】Android 中string-array的用法
在Android中,用string-array是一种简单的提取XML资源文件数据的方法. 例子如下: 把相应的数据放到values文件夹的arrays.xml文件里 <?xml version= ...
- Python_str 的内部功能介绍
float: x.as_integer_ratio():把浮点型转换成分数最简比 x.hex():返回当前值的十六进制表示 x.fromhex():将十六进制字符串转换为浮点型 float与long的 ...
- 【转】科大校长给数学系学弟学妹的忠告&本科数学参考书
1.老老实实把课本上的题目做完.其实说科大的课本难,我以为这话不完整.科大的教材,就数学系而言还是讲得挺清楚的,难的是后面的习题.事实上做1道难题的收获是做10道简单题所不能比的. 2.每门数学必修课 ...
- Clairewd’s message ekmp
给两个串第一个串是翻译表(密文可以通过翻译表翻译成明文),第二个串是由密文+明文组成,前面是密文(完整的),后面是明文(未必完整),问能不能把第二个串补全,输出最短的一种可能. 一开始 用的strin ...
- C++中全排列算法函数next_permutation的使用方法
首先,先看对next_permutation函数的解释: http://www.cplusplus.com/reference/algorithm/next_permutation/?kw=next_ ...
- 搭建TensorFlow中碰到的一些问题(TensorBoard不是内部或外部指令也不是可运行的程序)~
一.windows10环境+pip python软件包(最新版)+Pycharm软件(过段时间在弄下CUDA和GPU吧) 直接使用pip指令来安装tensorflow软件(如果很久没有更新pip软件包 ...