数据类型--列表

2018-06-17 23:52:41来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

 

列表概念

列表又称:list是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目。
List(列表) 是 Python 中使用最频繁的数据类型。列表可以完成大多数集合类的数据结构实现。
列表用 “ [  ] " 标识。是python最通用的复合数据类型,它支持字符,数字,字符串甚至可以包含列表(所谓嵌套)。
列表中的值得分割,也可以用到变量[头下标:尾下标],又称切片。
它可以截取相应的列表,从左到右索引默认0开始的,从右到左索引默认-1开始。
下标可以为空表示取到头或尾。
加号(+)是列表连接运算符,星号(*)是重复操作。

 

要点

列表是可变类型的数据。

列表的组成:用[]表示列表,包含了多个以逗号分隔开的数字,或者字符串。

列表与元祖的区别:操作基本一样,唯一区别就是元祖值不可变更。

例如:

1 list1=['Simon','David','Clotho','张三']
2 list2=[1,2,3,4,5]
3 list3=["str1","str2","str3","str4","str5"]

 

列表的用法

基本操作:

索引
切片
追加
删除
长度
切片
循环
包含

 1 list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
 2 tinylist = [123, 'john']
 3 
 4 print(list) # 输出完整列表
 5 print(list[0]) # 输出列表的第一个元素
 6 print(list[1:3]) # 输出第二个至第三个的元素
 7 print(list[2:]) # 输出从第三个开始至列表末尾的所有元素
 8 print(tinylist * 2) # 输出列表两次
 9 print(list + tinylist) # 打印组合的列表
10 
11 输出结果:
12 ['abcd', 786, 2.23, 'john', 70.2]
13 abcd
14 [786, 2.23]
15 [2.23, 'john', 70.2]
16 [123, 'john', 123, 'john']
17 ['abcd', 786, 2.23, 'john', 70.2, 123, 'john']

 

 

列表的其他参数

  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
列表源代码

 

列表的练习:

 

  1 names = ['a','b','c','d','e','f']
  2 ########切片########
  3 print(names[1:4])
  4 print(names[:4])
  5 print(names[2],names[4])
  6 print(names[1:3]) #起始位置包含本身,结束为不包含本身,需要往后+1位。
  7 print(names[0:3])
  8 print("取前单3个值",names[:3])
  9 print(names[-1]) #取最后一个
 10 print(names[-3:]) #取最后三个
 11 print(names[-2:]) #取最后两个
 12 
 13 显示结果:
 14 ['b', 'c', 'd']
 15 ['a', 'b', 'c', 'd']
 16 c e
 17 ['b', 'c']
 18 ['a', 'b', 'c']
 19 取前单3个值 ['a', 'b', 'c']
 20 f
 21 ['d', 'e', 'f']
 22 ['e', 'f']
 23 
 24 
 25 
 26 ########追加##########:
 27 print('='.center(50,'='))
 28 names.append("c")
 29 names.append("d")
 30 names.append("b")
 31 print(names)
 32 
 33 显示结果:
 34 ['a', 'b', 'c', 'd', 'e', 'f', 'c', 'd', 'b']
 35 
 36 
 37 
 38 
 39 ########指定位置插入########
 40 print('='.center(50,'='))
 41 names.insert(2,"w")
 42 names.insert(4,"k")
 43 names.insert(6,"w")
 44 names.insert(9,"r")
 45 names.insert(10,"w")
 46 names.insert(13,"w")
 47 print(names)
 48 
 49 显示结果:
 50 ['a', 'b', 'w', 'c', 'k', 'd', 'w', 'e', 'f', 'r', 'w', 'c', 'd', 'w', 'b']
 51 
 52 
 53 
 54 ########更改########
 55 print('='.center(50,'='))
 56 names[0] = 2
 57 names[3] = 3
 58 print(names)
 59 
 60 显示结果:
 61 [2, 'b', 'w', 3, 'k', 'd', 'w', 'e', 'f', 'r', 'w', 'c', 'd', 'w', 'b']
 62 
 63 
 64 
 65 ########删除########
 66 names.remove(2) #按照元素名称删除
 67 print(names)
 68 
 69 del names[2] #按照下标的位置删除
 70 print(names)
 71 
 72 names.pop() #从后往前删
 73 print(names)
 74 
 75 names.pop(7) #删除第7位的数值
 76 print(names)
 77 
 78 
 79 显示结果:
 80 ['b', 'w', 3, 'k', 'd', 'w', 'e', 'f', 'r', 'w', 'c', 'd', 'w', 'b']
 81 ['b', 'w', 'k', 'd', 'w', 'e', 'f', 'r', 'w', 'c', 'd', 'w', 'b']
 82 ['b', 'w', 'k', 'd', 'w', 'e', 'f', 'r', 'w', 'c', 'd', 'w']
 83 ['b', 'w', 'k', 'd', 'w', 'e', 'f', 'w', 'c', 'd', 'w']
 84 
 85 
 86 
 87 ########索引########
 88 print(names.index("w"))
 89 
 90 #表里的元素过多,但是只知道某一个元素的名字,然后通过元素的名字取索引的位置。
 91 print(names.index("d"))
 92 
 93 #已知上面的位置,找到相对应的名字。
 94 print(names[names.index("d")])
 95 
 96 ########查找重复元素的个数########
 97 print(names.count("w"))
 98 
 99 ########清理重复的字符########
100 for i in range(names.count("w")):
101     names.remove("w")
102 else:
103     print("循环删除",names)
104 
105 显示结果:
106 3
107 d
108 循环删除 ['b', 'k', 'd', 'e', 'f', 'c', 'd']
109 
110 
111 
112 ########清理列表########
113 names.clear()
114 print(names)
115 
116 显示结果:
117 []
118 
119 
120 
121 ########反转########
122 names.reverse()
123 print(names)
124 
125 ######排序########
126 names.insert(3,"#")
127 names.insert(8,"!@2@#")
128 print(names)
129 names.sort()
130 print(names)
131 
132 
133 显示结果:
134 ['d', 'c', 'f', 'e', 'd', 'k', 'b']
135 ['d', 'c', 'f', '#', 'e', 'd', 'k', 'b', '!@2@#']
136 ['!@2@#', '#', 'b', 'c', 'd', 'd', 'e', 'f', 'k']
137 
138 
139 
140 
141 ######反转########
142 name2 = [1,2,3,4]
143 names.extend(name2)
144 print(names,name2)
145 
146 names+name2
147 print(names,name2)
148 
149 显示结果:
150 ['!@2@#', '#', 'b', 'c', 'd', 'd', 'e', 'f', 'k', 1, 2, 3, 4] [1, 2, 3, 4]
151 ['!@2@#', '#', 'b', 'c', 'd', 'd', 'e', 'f', 'k', 1, 2, 3, 4] [1, 2, 3, 4]
152 
153 
154 names = ['a', 'b', '',['alex','jack'],'d', 'd', 'e', 'f']
155 
156 ######循环列表######
157 for i in names:
158     print(i)
159 
160 #######按照步长来切片######
161 #range(1,10,2)从1--10每个2个打印一次
162 print(names[0:-1:2])
163 
164 ######0和-1都可以省略掉######
165 print(names[::2])
166 
167 显示结果:
168 a
169 b
170 171 ['alex', 'jack']
172 d
173 d
174 e
175 f
176 ['a', '', 'd', 'e']
177 ['a', '', 'd', 'e']
View Code

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:数据类型--字典

下一篇:【Python爬虫实战】 使用代理服务器