Python基本数据类型(列表)
⼀、概述
列表(List)是⼀个有序的Python对象序列。
⼆、列表格式
列表可以⽤⼀对中括号“[ ]”⽣成,中间的元素⽤逗号“,”隔开:
li = [1,2,"alex"]
三、列表的运算
列表与字符串类似,⽀持相加和数乘。
列表相加,相当于将这两个列表按顺序连接:
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']
四、索引和切⽚
列表是个有序的序列,因此也⽀持索引和切⽚的操作。
索引:
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]
与字符串不同,我们可以通过索引和切⽚来修改列表。
五、元素的删除
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']
六、从属关系的判断
可以⽤关键字in和not in判断某个元素是否在某个序列中。例如:对于列表
li = [1,2,3,4,"alex"]
s = 2 in li
print(s) #结果为:True
七、列表的⽅法
列表⼀些常⽤的⽅法。
(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"]
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"]
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]
print(li) #结果为:[4, 5, 1, 6, 3]
(8).count⽅法
.count⽅法是计算列表的指定元素出现的次数。
li = [3,6,1,5,4,6]
unt(6)) #结果为:2
(9).index⽅法
.index⽅法是获取指定元素第⼀次出现的索引位置。
li = [3,6,1,5,4,6]
print(li.index(6)) #结果为:1
列表所有⽅法归纳:
1class list(object):
2"""
3 list() -> new empty list
4 list(iterable) -> new list initialized from iterable's items
5"""
6def append(self, p_object): # real signature unknown; restored from __doc__
7""" L.append(object) -- append object to end """
8pass
9
10def count(self, value): # real signature unknown; restored from __doc__
11""" L.count(value) -> integer -- return number of occurrences of value """
12return 0
13
14def extend(self, iterable): # real signature unknown; restored from __doc__
15""" L.extend(iterable) -- extend list by appending elements from the iterable """
16pass
17
18def 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"""
23return 0
24
25def insert(self, index, p_object): # real signature unknown; restored from __doc__
26""" L.insert(index, object) -- insert object before index """
27pass
28
29def 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"""
34pass
35
36def 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"""
41pass
42
43def reverse(self): # real signature unknown; restored from __doc__
44""" L.reverse() -- reverse *IN PLACE* """
45pass
46
47def 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"""
52pass
53
54def__add__(self, y): # real signature unknown; restored from __doc__
55""" x.__add__(y) <==> x+y """
56pass
57
58def__contains__(self, y): # real signature unknown; restored from __doc__
59""" x.__contains__(y) <==> y in x """
60pass
61
62def__delitem__(self, y): # real signature unknown; restored from __doc__
63""" x.__delitem__(y) <==> del x[y] """
64pass
65
66def__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"""
72pass
73
74def__eq__(self, y): # real signature unknown; restored from __doc__
75""" x.__eq__(y) <==> x==y """
76pass
77
78def__getattribute__(self, name): # real signature unknown; restored from __doc__
79""" x.__getattribute__('name') <==> x.name """
80pass
81
82def__getitem__(self, y): # real signature unknown; restored from __doc__
83""" x.__getitem__(y) <==> x[y] """
84pass
85
86def__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"""
92pass
93
94def__ge__(self, y): # real signature unknown; restored from __doc__
95""" x.__ge__(y) <==> x>=y """
96pass
97
98def__gt__(self, y): # real signature unknown; restored from __doc__
99""" x.__gt__(y) <==> x>y """
100pass
101
102def__iadd__(self, y): # real signature unknown; restored from __doc__ 103""" x.__iadd__(y) <==> x+=y """
104pass
105
106def__imul__(self, y): # real signature unknown; restored from __doc__ 107""" x.__imul__(y) <==> x*=y """
108pass
109
110def__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"""
116pass
117
118def__iter__(self): # real signature unknown; restored from __doc__ 119""" x.__iter__() <==> iter(x) """
120pass
121
122def__len__(self): # real signature unknown; restored from __doc__ 123""" x.__len__() <==> len(x) """
124pass
125
126def__le__(self, y): # real signature unknown; restored from __doc__ 127""" x.__le__(y) <==> x<=y """
128pass
129
130def__lt__(self, y): # real signature unknown; restored from __doc__ 131""" x.__lt__(y) <==> x<y """
132pass
133
134def__mul__(self, n): # real signature unknown; restored from __doc__ 135""" x.__mul__(n) <==> x*n """
136pass
137
138 @staticmethod # known case of __new__
139def__new__(S, *more): # real signature unknown; restored from __doc__ 140""" T.__new__(S, ...) -> a new object with type S, a subtype of T """
141pass
142
143def__ne__(self, y): # real signature unknown; restored from __doc__ 144""" x.__ne__(y) <==> x!=y """
145pass
146
147def__repr__(self): # real signature unknown; restored from __doc__ 148""" x.__repr__() <==> repr(x) """
149pass
150
151def__reversed__(self): # real signature unknown; restored from __doc__ 152""" L.__reversed__() -- return a reverse iterator over the list """
153pass
154
155def__rmul__(self, n): # real signature unknown; restored from __doc__ 156""" x.__rmul__(n) <==> n*x """
157pass
158
159def__setitem__(self, i, y): # real signature unknown; restored from __doc__ 160""" x.__setitem__(i, y) <==> x[i]=y """
getattribute方法返回类型161pass
162
163def__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"""
169pass
170
171def__sizeof__(self): # real signature unknown; restored from __doc__ 172""" L.__sizeof__() -- size of L in memory, in bytes """
173pass
174
175__hash__ = None
176
177 list
list
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论