1.列表list[]----可以包含多种数据对象,相同或不相同都可以。
list1=[1,2,‘ok‘,[1,2,3,4,5],True,‘瑞文‘] In[15]: list1 Out[15]: [1, 2, ‘ok‘, [1, 2, 3, 4, 5], True, ‘瑞文‘]
2.列表是序列----所以有索引、切片
a=[1,2,3,4,5] In[35]: a[2] Out[35]: 3 In[36]: a[1:4:2] Out[36]: [2, 4] In[39]: a[4:1:-2] Out[39]: [5, 3] In[40]: a[:] Out[40]: [1, 2, 3, 4, 5] In[41]: a[::-1] #反转列表同(a.reverse()) Out[41]: [5, 4, 3, 2, 1]
3.列表可以修改-----与字符串、元祖最大区别
a=[1,2,3,4,5] In[43]: a[0]=6 In[44]: a Out[44]: [6, 2, 3, 4, 5]
4.列表常用函数----重要
dir(list) Out[45]: [‘__add__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__gt__‘, ‘__hash__‘, ‘__iadd__‘, ‘__imul__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__iter__‘, ‘__le__‘, ‘__len__‘, ‘__lt__‘, ‘__mul__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__reversed__‘, ‘__rmul__‘, ‘__setattr__‘, ‘__setitem__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘append‘, ‘clear‘, ‘copy‘, ‘count‘, ‘extend‘, ‘index‘, ‘insert‘, ‘pop‘, ‘remove‘, ‘reverse‘, ‘sort‘]
重要的:append、clear、copy、count、extend、index、insert、pop、remove、reverse、sort
下一节给出
时间: 2024-11-07 05:01:38