Python 列表list使用介绍
一组有序项目的集合 可变的数据类型【可进行增删改查】 列表中可以包含任何数据类型,也可包含另一个列表【可任意组合嵌套】 列表是以方括号“[]”包围的数据集合,不同成员以“,”分隔 列表可通过序号访问其中成员
定义
>>>l=[]#空列表 >>>l=[1,2,3] >>>l=[1,2,3,['a','b']] >>>l=list('linuxeye') >>>l ['l','i','n','u','x','e','y','e'] >>>l=list(range(5)) >>>l [0,1,2,3,4] >>>l='1;2;3;4;5'.split(';') >>>l ['1','2','3','4','5']
内建函数list(a_sequence)可以将一个序列转为列表
索引
正索引是编号是从左到右,0开始;负索引编号是从右往左,-1、-2…;取范围值都是从左至右
>>>LanguageList=['Python','Shell','Perl','PHP','java','C',"C++","Lisp"] >>>LanguageList[0]#取第一个值 'Python' >>>LanguageList[3]#第四个值 'PHP' >>>LanguageList[3:] ['PHP','java','C','C++','Lisp'] >>>LanguageList[:3] ['Python','Shell','Perl'] >>>LanguageList[2:5] ['Perl','PHP','java']
>>>LanguageList[-1]#倒数第一个值 'Lisp' >>>LanguageList[-2]#倒数第二个值 'C++' >>>LanguageList[-5:] ['PHP','java','C','C++','Lisp'] >>>LanguageList[-5:-2] ['PHP','java','C'] >>>LanguageList[:-2] ['Python','Shell','Perl','PHP','java','C']
增加元素
>>>LanguageList ['Python','Shell','Perl','PHP','java','C','C++','Lisp'] >>>LanguageList.append('GO') >>>LanguageList ['Python','Shell','Perl','PHP','java','C','C++','Lisp','GO'] >>>LanguageList.insert(3,'Python')#往PHP元素前插入 >>>LanguageList ['Python','Shell','Perl','Python','PHP','java','C','C++','Lisp','GO'] >>>LanguageList.extend(['Ruby','C#']) >>>LanguageList ['Python','Shell','Perl','Python','PHP','java','C','C++','Lisp','GO','Ruby','C#']
搜索
>>>LanguageList=['Python','Shell','Perl','Python','PHP','java','C','C++','Lisp','GO','Ruby','C#'] >>>LanguageList.index('Python') 0 >>>LanguageList.index('C') 6 >>>LanguageList.index('c') Traceback(mostrecentcalllast): File"<stdin>",line1,in<module> ValueError:list.index(x):xnotinlist >>>'c'inLanguageList False >>>LanguageList.index('Python')#统计一个元素的出现次数 2
删除元素
>>>LanguageList=['Python','Shell','Perl','Python','PHP','java','C','C++','Lisp','GO','Ruby','C#'] >>>LanguageList.remove('C') >>>LanguageList.remove('Python') >>>LanguageList.remove('c') Traceback(mostrecentcalllast): File"<stdin>",line1,in<module> ValueError:list.remove(x):xnotinlist >>>LanguageList.pop() 'C#' >>>LanguageList ['Shell','Perl','Python','PHP','java','C++','Lisp','GO','Ruby'] >>>LanguageList.pop(-2) ['Shell','Perl','Python','PHP','java','C++','Lisp','Ruby'] >>>delLanguageList[-3:-1] >>>LanguageList ['Shell','Perl','Python','PHP','java','Ruby']
remove从列表中只删除一个值的首次出现。
pop会做两件事:删除列表的最后一个元素,然后返回删除元素的值。
修改元素
>>>LanguageList=['Python','Shell','Perl','Python','PHP','java','Ruby'] >>>LanguageList[-2]='C' >>>LanguageList ['Python','Shell','Perl','Python','PHP','C','Ruby'] >>>LanguageList[LanguageList.index('C')]='JAVA' >>>LanguageList ['Python','Shell','Perl','Python','PHP','JAVA','Ruby']
运算符
>>>LanguageList ['Shell','Perl','Python','PHP','java','C++','Lisp','GO','Ruby'] >>>LanguageList=LanguageList+['Python','C'] >>>LanguageList ['Shell','Perl','Python','PHP','java','C++','Lisp','GO','Ruby','Python','C'] >>>LanguageList+=['java'] >>>LanguageList ['Shell','Perl','Python','PHP','java','C++','Lisp','GO','Ruby','Python','C','java'] >>>NumList=[1,2,3]*3 >>>NumList [1,2,3,1,2,3,1,2,3]
排序
>>>NumList=[2,5,3,6,1,4] >>>NumList.reverse() >>>NumList [4,1,6,3,5,2] >>>NumList[::-1]#和reverse效果相同 >>>NumList [2,5,3,6,1,4] >>>NumList.sort() >>>NumList [1,2,3,4,5,6]
sort可接受参数
cmp,比较函数,接受两个参数,小于时返回负,大于返回正,相等返回0 key,指定排序键 reverse,指定是否反序
列表的比较操作,隐式调用cmp方法,比较规则是逐个扫描元素进行比较,如果可以比较,则比较,如果相等扫描下一个元素,如果不相等返回结果,如果两个元素类型不可以比较,就比较两个对象的id()值。如果一直相等,直到一个列表扫描结束,那么返回较长的列表较大
>>>L=[(13,54),(11,59),(15,55),(12,57),(10,56)] >>>L.sort(key=lambdax:x[1]) >>>L [(13,54),(15,55),(10,56),(12,57),(11,59)] >>>L.sort(key=lambdax:x[1],reverse=1)#或者reverse=True >>>L [(11,59),(12,57),(10,56),(15,55),(13,54)]
sort与sorted函数区别
NumList=[2,5,3,6,1,4] >>>sorted(NumList) [1,2,3,4,5,6] >>>NumList [2,5,3,6,1,4] >>>NumList.sort() >>>NumList [1,2,3,4,5,6]
sort:在原list上排序,不返回排序后的list
sorted:不改变原list,返回排序后的list