python 中关于列表的操作。
列表是python中内置的类型中序列类型中的第一个,Sequence Types — list, tuple, range。
一,这三种序列类型通用的语句是
- operation
- x in s True if an item of s is equal to x,else False
- x not in s False if an item of s is equal to x,else True
- s + t the concatenation of s and t
- s * n or n * s equicalent to adding s to itself n times
- s[i] item of s,origin()
- len(s)
- min()
- s.index(x[, i[, j]]) index of the first occurrence of x in s (at of after index i and before index j),raise error when x is not found
- s.count(x) total number of occurrences of x in s
例1:
print(3 in [1, 2, 3])
print(4 in range(8))
print(‘laura’ in (‘wendy’, ‘laura’, ‘tony’))
print((‘laura’, ‘wendy’) in (‘laura’, ‘wendy’, ‘ilen’))
运行结果如下
True
True
True
False
例2:
print([9, 3, 4] + [5, 2, 9])
print((‘laura’, ‘sandu’) + (‘ilen’, ‘iris’))
print([8, 5] * 5)
print((1, 4, 6) * 3)
print((3, 5, 2, 9)[2:])
lists = [[]] * 3
print(lists)
运行结果如下
[9, 3, 4, 5, 2, 9]
(‘laura’, ‘sandu’, ‘ilen’, ‘iris’)
[8, 5, 8, 5, 8, 5, 8, 5, 8, 5]
(1, 4, 6, 1, 4, 6, 1, 4, 6)
(2, 9)
[[], [], []]
例3:
lists = [[]] * 3
lists
[[], [], []]
lists[0].append(3) # 这个列表里面的元素是被引用的,不是被复制的,如果一个变了所有都会变,类似浅copy,被引用了三次而已
lists
[[3], [3], [3]]
二,可以变的数据类型的操作,就是数据是可以改变的时候的操作s[i] = x item i of s is repleced by x s[i:j] = t slice of s from i to j is replaced by the contents of the iterable t def s[i:j] same as s[i:j] = [] s[i:j:k] = t the elements of s[i:j:k] are replaced by those of t, t must habe the same length as the slice it is replacing del s[i:j:k] removes the elements of s[i:j:k] from the list s.append(x) appends x to the end of the sequence (sanme as s[len(s):len(s)] = [x] s.clear() removes all items from s(same as del s[:]) s.copy() creates a shallow copy of s(same as s [:] s.extend(t) or s +=t extends s with the contents of t(for the most part the same as s[len(s):len(s)] = t) s * = n updates s with its contents repeated n times s.insert(i, x) insert x into s at he index given by i (same as s[I:I] = [x] s.pop([i]) retrieves the item at i and also removes it froms s.remove(x) remove the first item from s wheres[i] ==x s.reverse() reverses the items of s in place
三、List
List are mutable sequences,列表可储存很多数据。列表是一个可迭代的一个类。有很多种方法可以创建列表,如:
print([])
print([[54], [‘laura’], [84]])
print([i for i in range(4)]) # list comprehension
print(list((9, 5, 3, (5, 3))))
[]
[[54], [‘laura’], [84]]
[0, 1, 2, 3]
[9, 5, 3, (5, 3)]
列表可以使用上述的共同的和可变数据联系的操作方法。列表还提供了下面这种附加的方法
sort(*,key=None, reverse=False)例:
l = [9, 5, 4, 3, 8, 2, 6]
l.sort()
print(l)
l.sort(reverse=True) # 降序
print(l)[2, 3, 4, 5, 6, 8, 9]
[9, 8, 6, 5, 4, 3, 2]
四、Tuples
元组是不可变的序列,通常用于储存非寻常的数据类型,如由enumerate()这个内置函数生成的2元组。或者一些不能改变的数据存储,如集合或dict种的实例。元组也是一个类。可以通过以下方法建立:
print(())
print((4,))
print((4, 4, 3, 9))
print(tuple((9, 3, 5)))
print(tuple(‘abc/’)) # 可以是一个序列
print(tuple([1, 2, 3])) # 可以是一个可迭代的容器
>
()
(4,)
(4, 4, 3, 9)
(9, 3, 5)
(‘a’, ‘b’, ‘c’, ‘/’)
(1, 2, 3)
元组可以使用上述公同的序列操作方法,下面的可变的数据类型操作方法无法使用。
五、Ranges
range类型的是不可变的数据类型。通常用于for循环。
range是一个类range(start, stop[,step])
range的参数一定要是整数。像这种实现了next() 特殊方法的的对象,都要求写整数。例
r = range(0, 20, 2)
print(r)
print(10 in r)
print(r.index(10))
print(r[5])
print(r[:5])
print(r[-1])
print(range(0) == range(2, 1, 3))
print(range(0, 3, 2) == range(0, 4, 2)
>
range(0, 20, 2)
True
5
10
range(0, 10, 2)
18
True
True