Python基本数据类型(列表)
2020-12-13 02:21
标签:spec 格式 生成 ast 一个 关系 stop with 次数 基本数据类型 三、列表 列表(List)是一个有序的Python对象序列。 1.列表格式 列表可以用一对中括号“[ ]”生成,中间的元素用逗号“,”隔开: 2.列表的运算 列表与字符串类似,支持相加和数乘。 列表相加,相当于将这两个列表按顺序连接: 列表数乘,相当于将这个列表重复多次: 3.索引和切片 列表是个有序的序列,因此也支持索引和切片的操作。 索引: 切片: 与字符串不同,我们可以通过索引和切片来修改列表。 4.元素的删除 Python提供了一种更通用的删除列表元素的方法:关键字del。 5.从属关系的判断 可以用关键字in和not in判断某个元素是否在某个序列中。例如:对于列表 6.列表的方法 列表一些常用的方法。 (1).append()方法 .append()方法向列表追加单个元素。 (2).extend方法 .extend方法将另一个序列中的元素依次添加到列表。 (3).insert方法 .insert方法在指定索引位置插入元素。 (4).remove方法 .remove方法将列表中第一个出现的指定元素删除,指定元素不存在会报错。 (5).pop方法 .pop方法将列表中指定索引的元素删除,默认最后一个元素,并返回这个元素值。 (6).sort方法 .sort方法将列表中的元素按照从小到大排序。 可以在调用时加入一个reverse参数,如果它等于True,列表会按照从大到小进行排序: (7).reverse()方法 .reverse()方法将列表中的元素进行翻转。 (8).count方法 .count方法是计算列表的指定元素出现的次数。 (9).index方法 .index方法是获取指定元素第一次出现的索引位置。 列表所有方法归纳: Python基本数据类型(列表) 标签:spec 格式 生成 ast 一个 关系 stop with 次数 原文地址:https://www.cnblogs.com/lzc69/p/11032430.htmlli = [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]
li = [1,2,3,4,"alex"]
del li[1]
print(li) #结果为:[1, 3, 4, ‘alex‘]
del li[1:-1]
print(li) #结果为:[1, ‘alex‘]
li = [1,2,3,4,"alex"]
s = 2 in li
print(s) #结果为:True
li = [1,2,3,4,"alex"]
li.append(5)
print(li) #结果为:[1, 2, 3, 4, ‘alex‘, 5]
li = [1,2,3,4,"alex"]
li.extend([5,8])
print(li) #结果为:[1, 2, 3, 4, ‘alex‘, 5, 8]
li = [1,2,3,4,"alex"]
li.insert(2,"hello")
print(li) #结果为:[1, 2, ‘hello‘, 3, 4, ‘alex‘]
li = [1,2,3,4,"alex"]
li.remove(3)
print(li) #结果为:[1, 2, 4, ‘alex‘]
li = [1,2,3,4,"alex"]
a = li.pop(3)
print(li) #结果为:[1, 2, 3, ‘alex‘]
print(a) #结果为:4
li = [3,6,1,5,4]
li.sort()
print(li) #结果为:[1, 3, 4, 5, 6]
li = [3,6,1,5,4]
li.sort(reverse=True)
print(li) #结果为:[6, 5, 4, 3, 1]
li = [3,6,1,5,4]
li.reverse()
print(li) #结果为:[4, 5, 1, 6, 3]
li = [3,6,1,5,4,6]
print(li.count(6)) #结果为:2
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"""
128 pass
129
130 def __lt__(self, y): # real signature unknown; restored from __doc__
131 """ x.__lt__(y) x