-
列表list
-
元组tuple
-
集合set
-
字典dictionary
-
字符串string
一、列表list
list :python的一种数据类型。是可变的,有序的对象。可以删除和增加元素。
1 列表常用的几种方法
list.append(x)
list.extend(iterable)
>>> ls=[1,2,'a',3] >>> ls.append('b') >>> ls [1, 2, 'a', 3, 'b'] >>> t=(3,7) >>> ls.extend(t) >>> ls [1, 2, 'a', 3, 'b', 3, 7] >>>
list.remove(x) 从列表中删除x 元素
list.pop(i) 返回删除的元素的值,i为索引号 ,如果不指定参数,默认从列表最后开始删除元素
list.insert(i,x) 指定在某个索引号插入一个元素x
list.count(x) 计算x 元素在列表中出现的次数
list.index(x) 返回x 元素在列表中出现的索引号,如果列表中有多个x, 返回第一个
list.copy() 复制这个列表,会产生新的对象
>>> a=ls.copy() >>> a [2, 'a', 3, 7, 3] >>> id(a) 54370632 >>> a.ppend('s') Traceback (most recent call last): File "<pyshell#22>", line 1, in <module> a.ppend('s') AttributeError: 'list' object has no attribute 'ppend' >>> a.append('s') >>> a [2, 'a', 3, 7, 3, 's'] >>> ls [2, 'a', 3, 7, 3] >>>