基本数据类型
三、列表
列表(List)是一个有序的Python对象序列。
1.列表格式
列表可以用一对中括号“[ ]”生成,中间的元素用逗号“,”隔开:
li = [1,2,"alex"]
2.列表的运算
列表与字符串类似,支持相加和数乘。
列表相加,相当于将这两个列表按顺序连接:
li = [1,2,3] + ["alex",5] print(li) #结果为:[1,2,3,"alex",5]
列表数乘,相当于将这个列表重复多次:
li = [1,2,"alex"] * 2 print(li) #结果为:[1, 2, 'alex', 1, 2, 'alex']
3.索引和切片
列表是个有序的序列,因此也支持索引和切片的操作。
索引:
li = [1,2,3,4,"alex"] print(li[0]) #结果为:1 print(li[-1]) #结果为:alex
切片:
li = [1,2,3,4,"alex"] print(li[2:-1]) #结果为:[3,4]
与字符串不同,我们可以通过索引和切片来修改列表。
4.元素的删除
Python提供了一种更通用的删除列表元素的方法:关键字del。
li = [1,2,3,4,"alex"] del li[1] print(li) #结果为:[1, 3, 4, 'alex'] del li[1:-1] print(li) #结果为:[1, 'alex']
5.从属关系的判断
可以用关键字in和not in判断某个元素是否在某个序列中。例如:对于列表
li = [1,2,3,4,"alex"] s = 2 in li print(s) #结果为:True
6.列表的方法
列表一些常用的方法。
(1).append()方法
.append()方法向列表追加单个元素。
li = [1,2,3,4,"alex"] li.append(5) print(li) #结果为:[1, 2, 3, 4, 'alex', 5]
(2).extend方法
.extend方法将另一个序列中的元素依次添加到列表。
li = [1,2,3,4,"alex"] li.extend([5,8]) print(li) #结果为:[1, 2, 3, 4, 'alex', 5, 8]
(3).insert方法
.insert方法在指定索引位置插入元素。
li = [1,2,3,4,"alex"] li.insert(2,"hello") print(li) #结果为:[1, 2, 'hello', 3, 4, 'alex']
(4).remove方法
.remove方法将列表中第一个出现的指定元素删除,指定元素不存在会报错。
li = [1,2,3,4,"alex"] li.remove(3) print(li) #结果为:[1, 2, 4, 'alex']
(5).pop方法
.pop方法将列表中指定索引的元素删除,默认最后一个元素,并返回这个元素值。
li = [1,2,3,4,"alex"] a = li.pop(3) print(li) #结果为:[1, 2, 3, 'alex'] print(a) #结果为:4
(6).sort方法
.sort方法将列表中的元素按照从小到大排序。
li = [3,6,1,5,4] li.sort() print(li) #结果为:[1, 3, 4, 5, 6]
可以在调用时加入一个reverse参数,如果它等于True,列表会按照从大到小进行排序:
li = [3,6,1,5,4] li.sort(reverse=True) print(li) #结果为:[6, 5, 4, 3, 1]
(7).reverse()方法
.reverse()方法将列表中的元素进行翻转。
li = [3,6,1,5,4] li.reverse() print(li) #结果为:[4, 5, 1, 6, 3]
(8).count方法
.count方法是计算列表的指定元素出现的次数。
li = [3,6,1,5,4,6] print(li.count(6)) #结果为:2
(9).index方法
.index方法是获取指定元素第一次出现的索引位置。
li = [3,6,1,5,4,6] print(li.index(6)) #结果为:1
列表所有方法归纳:
1 class list(object): 2 """ 3 list() -> new empty list 4 list(iterable) -> new list initialized from iterable's items 5 """ 6 def append(self, p_object): # real signature unknown; restored from __doc__ 7 """ L.append(object) -- append object to end """ 8 pass 9 10 def count(self, value): # real signature unknown; restored from __doc__ 11 """ L.count(value) -> integer -- return number of occurrences of value """ 12 return 0 13 14 def extend(self, iterable): # real signature unknown; restored from __doc__ 15 """ L.extend(iterable) -- extend list by appending elements from the iterable """ 16 pass 17 18 def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ 19 """ 20 L.index(value, [start, [stop]]) -> integer -- return first index of value. 21 Raises ValueError if the value is not present. 22 """ 23 return 0 24 25 def insert(self, index, p_object): # real signature unknown; restored from __doc__ 26 """ L.insert(index, object) -- insert object before index """ 27 pass 28 29 def pop(self, index=None): # real signature unknown; restored from __doc__ 30 """ 31 L.pop([index]) -> item -- remove and return item at index (default last). 32 Raises IndexError if list is empty or index is out of range. 33 """ 34 pass 35 36 def remove(self, value): # real signature unknown; restored from __doc__ 37 """ 38 L.remove(value) -- remove first occurrence of value. 39 Raises ValueError if the value is not present. 40 """ 41 pass 42 43 def reverse(self): # real signature unknown; restored from __doc__ 44 """ L.reverse() -- reverse *IN PLACE* """ 45 pass 46 47 def sort(self, cmp=None, key=None, reverse=False): # real signature unknown; restored from __doc__ 48 """ 49 L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*; 50 cmp(x, y) -> -1, 0, 1 51 """ 52 pass 53 54 def __add__(self, y): # real signature unknown; restored from __doc__ 55 """ x.__add__(y) <==> x+y """ 56 pass 57 58 def __contains__(self, y): # real signature unknown; restored from __doc__ 59 """ x.__contains__(y) <==> y in x """ 60 pass 61 62 def __delitem__(self, y): # real signature unknown; restored from __doc__ 63 """ x.__delitem__(y) <==> del x[y] """ 64 pass 65 66 def __delslice__(self, i, j): # real signature unknown; restored from __doc__ 67 """ 68 x.__delslice__(i, j) <==> del x[i:j] 69 70 Use of negative indices is not supported. 71 """ 72 pass 73 74 def __eq__(self, y): # real signature unknown; restored from __doc__ 75 """ x.__eq__(y) <==> x==y """ 76 pass 77 78 def __getattribute__(self, name): # real signature unknown; restored from __doc__ 79 """ x.__getattribute__('name') <==> x.name """ 80 pass 81 82 def __getitem__(self, y): # real signature unknown; restored from __doc__ 83 """ x.__getitem__(y) <==> x[y] """ 84 pass 85 86 def __getslice__(self, i, j): # real signature unknown; restored from __doc__ 87 """ 88 x.__getslice__(i, j) <==> x[i:j] 89 90 Use of negative indices is not supported. 91 """ 92 pass 93 94 def __ge__(self, y): # real signature unknown; restored from __doc__ 95 """ x.__ge__(y) <==> x>=y """ 96 pass 97 98 def __gt__(self, y): # real signature unknown; restored from __doc__ 99 """ x.__gt__(y) <==> x>y """ 100 pass 101 102 def __iadd__(self, y): # real signature unknown; restored from __doc__ 103 """ x.__iadd__(y) <==> x+=y """ 104 pass 105 106 def __imul__(self, y): # real signature unknown; restored from __doc__ 107 """ x.__imul__(y) <==> x*=y """ 108 pass 109 110 def __init__(self, seq=()): # known special case of list.__init__ 111 """ 112 list() -> new empty list 113 list(iterable) -> new list initialized from iterable's items 114 # (copied from class doc) 115 """ 116 pass 117 118 def __iter__(self): # real signature unknown; restored from __doc__ 119 """ x.__iter__() <==> iter(x) """ 120 pass 121 122 def __len__(self): # real signature unknown; restored from __doc__ 123 """ x.__len__() <==> len(x) """ 124 pass 125 126 def __le__(self, y): # real signature unknown; restored from __doc__ 127 """ x.__le__(y) <==> x<=y """ 128 pass 129 130 def __lt__(self, y): # real signature unknown; restored from __doc__ 131 """ x.__lt__(y) <==> x<y """ 132 pass 133 134 def __mul__(self, n): # real signature unknown; restored from __doc__ 135 """ x.__mul__(n) <==> x*n """ 136 pass 137 138 @staticmethod # known case of __new__ 139 def __new__(S, *more): # real signature unknown; restored from __doc__ 140 """ T.__new__(S, ...) -> a new object with type S, a subtype of T """ 141 pass 142 143 def __ne__(self, y): # real signature unknown; restored from __doc__ 144 """ x.__ne__(y) <==> x!=y """ 145 pass 146 147 def __repr__(self): # real signature unknown; restored from __doc__ 148 """ x.__repr__() <==> repr(x) """ 149 pass 150 151 def __reversed__(self): # real signature unknown; restored from __doc__ 152 """ L.__reversed__() -- return a reverse iterator over the list """ 153 pass 154 155 def __rmul__(self, n): # real signature unknown; restored from __doc__ 156 """ x.__rmul__(n) <==> n*x """ 157 pass 158 159 def __setitem__(self, i, y): # real signature unknown; restored from __doc__ 160 """ x.__setitem__(i, y) <==> x[i]=y """ 161 pass 162 163 def __setslice__(self, i, j, y): # real signature unknown; restored from __doc__ 164 """ 165 x.__setslice__(i, j, y) <==> x[i:j]=y 166 167 Use of negative indices is not supported. 168 """ 169 pass 170 171 def __sizeof__(self): # real signature unknown; restored from __doc__ 172 """ L.__sizeof__() -- size of L in memory, in bytes """ 173 pass 174 175 __hash__ = None 176 177 listlist