python 中序列型別的操作
列表是python中內建的型別中序列型別,Sequence Types — list, tuple, range。python中序列型別包括列表,元組,範圍
一,這三種序列型別通用的語句
- 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]]
二,可變資料型別操作,也就是列表的操作
11. s[i] = x item i of s is repleced by x
12. s[i:j] = t slice of s from i to j is replaced by the contents of the iterable t
13. def s[i:j] same as s[i:j] = []
14. 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
15. del s[i:j:k] removes the elements of s[i:j:k] from the list
16. s.append(x) appends x to the end of the sequence (sanme as s[len(s):len(s)] = [x]
17. s.clear() removes all items from s(same as del s[:])
18. s.copy() creates a shallow copy of s(same as s [:]
19. 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)
20. s * = n updates s with its contents repeated n times
21. s.insert(i, x) insert x into s at he index given by i (same as s[I:I] = [x]
22. s.pop([i]) retrieves the item at i and also removes it froms
23. s.remove(x) remove the first item from s wheres[i] ==x
24. 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
把列表轉化成字串