#列表
1.数组:存储同一数据类型的集和。scores=[12,95,5]
2.列表:打了激素的数组:可以存储任意数据类型的集和。
例:li=[1,1.2,2+3j,True,"hello"]
print(li,type(li)) :list
3.列表的特性:s=['http','ssh','ftp']
索引:
正向索引:print(s(0)) : 'http'
反向索引:print(s(0)) : 'ftp'
切片:
print(s[::-1]) : 'ftp','ssh','http' # 列表的反转
print(s[1:]) : 'ssh','ftp' # 除了第一个之外的其他元素
print(s[:-1]) : 'http','ssh' # 除了最后一个之外的其他元素
重复:
print(s*3) :['http', 'ssh', 'ftp', 'http', 'ssh', 'ftp', 'http', 'ssh', 'ftp']
连接:
s1=['mysql','firewalld']
print(s+s1):['http', 'ssh', 'ftp', 'mysql', 'firewalld']
成员操作符:
print('firewalld' in s):False
print('firewalld' not in s):True
4.列表里嵌套列表:
List=[['fentiao',80],['fendai',90],['fensi,100']]
索引:print(List[0][0]) :fentiao
切片:print(List[:-1][0]): ['fentiao',80]
5.列表也有它的各种常用方法:
同字符串,我们可以用 help() 或者 dir() 方法得到它的各类方法
dir(List)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
6.通过5,详细讨论列表的增,删,改,查
--------增加:追加(加到列表末尾)一个元素(原样增加):List.append(('mianbao',101))
print(List) :[['fentiao',80],['fendai',90],['fensi,100'],('miaobao',101)]
增加多个元素:List.extend(['qiaokeli',102])
print(List) :[['fentiao',80],['fendai',90],['fensi,100'],('miaobao',101),'qiaokeli',102]
指定位置增加元素:List.inert(0,'xiaokeai')
print(List):['xiaokeai', ['fentiao', 80], ['fendai', 90], ['fensi,100'], ('miaobao', 101), 'qiaokeli', 102]
--------删除:List.remove(列表内元素名):List.remove('xiaokeai')
print(List) : [['fentiao',80],['fendai',90],['fensi,100'],('miaobao',101),'qiaokeli',102]
List.pop(0) :返回索引为0的元素,:['fentiao',80],并删除
print(List) : [['fendai',90],['fensi,100'],('miaobao',101),'qiaokeli',102]
List.remove(L[?])(通过索引删除):List.remove(List[0])
print(List):[['fensi,100'], ('miaobao', 101), 'qiaokeli', 102, ['dakeai', 105]]
直接删除列表:del(List):直接删除了列表。
--------修改:即重新复制:
List[-2]=110
print(List) :[['fensi,100'], ('miaobao', 101), 'qiaokeli', 110, ['dakeai', 105]]
--------查看:查看列表元素的下标用index方法
List.index(110) :3
查看某个列表元素出现的次数用count方法
List.count(110) :1