python基礎複習 - 列表基本操作
阿新 • • 發佈:2018-11-10
1 class list(object): 2 """ 3 Built-in mutable sequence. 4 5 If no argument is given, the constructor creates a new empty list. 6 The argument must be an iterable if specified. 7 """ 8 def append(self, *args, **kwargs): # real signature unknown 9 """list classAppend object to the end of the list. """ 10 pass 11 12 def clear(self, *args, **kwargs): # real signature unknown 13 """ Remove all items from list. """ 14 pass 15 16 def copy(self, *args, **kwargs): # real signature unknown 17 """ Return a shallow copy of the list.""" 18 pass 19 20 def count(self, *args, **kwargs): # real signature unknown 21 """ Return number of occurrences of value. """ 22 pass 23 24 def extend(self, *args, **kwargs): # real signature unknown 25 """ Extend list by appending elements from the iterable.""" 26 pass 27 28 def index(self, *args, **kwargs): # real signature unknown 29 """ 30 Return first index of value. 31 32 Raises ValueError if the value is not present. 33 """ 34 pass 35 36 def insert(self, *args, **kwargs): # real signature unknown 37 """ Insert object before index. """ 38 pass 39 40 def pop(self, *args, **kwargs): # real signature unknown 41 """ 42 Remove and return item at index (default last). 43 44 Raises IndexError if list is empty or index is out of range. 45 """ 46 pass 47 48 def remove(self, *args, **kwargs): # real signature unknown 49 """ 50 Remove first occurrence of value. 51 52 Raises ValueError if the value is not present. 53 """ 54 pass 55 56 def reverse(self, *args, **kwargs): # real signature unknown 57 """ Reverse *IN PLACE*. """ 58 pass 59 60 def sort(self, *args, **kwargs): # real signature unknown 61 """ Stable sort *IN PLACE*. """ 62 pass
插入
#插入 insert a = [i for i in range(3)] a.insert(1,4) print(a) #[0, 4, 1, 2]
#extend a = [0,1] a.extend([2,3]) print(a) #[0,1,2,3]
刪除
#pop a = [3,4,5] deleted_element = a.pop(1) print(deleted_element) #4 print(a) #[3,5]
#pop預設刪除最後一個元素 a = [1,2,3] deleted_item = a.pop() print(deleted_item) #3 print(a) #[1,2]
#remove 按元素刪除 a = [1,2,2,3] a.remove(2) print(a) #[1,2,3]
#clear 清空 a = [1,2,3] a.clear() print(a) #[]
#del 刪除 a = [1,2,3] del a print(a) #NameError: name 'a' is not defined
#del 切片刪除 a = [1,2,3,4] del a[1:] print(a) #[1]
將字串轉化為列表
#list a = '12' a = list(a) print(a) #['1','2'] #切片 a = [] a[0:2] = '12' print(a) #['1','2'] #以上就算序號超過字串長度也沒關係 a = [] a[0:3] = '12' print(a) #['1','2']
#字串長度超過所給序號也沒關係g a = [] a[0:2]='1234' print(a) #['1', '2', '3', '4']
#對於列表賦值也是一樣 a = [] a[0:1] = [i for i in range(10)] print(a) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
公共方法
len, count
排序
#sorted 不改變原字串順序,sort改變原字串順序,其他一樣 import random a = [i for i in range(10)] random.shuffle(a) print(a) #[7, 4, 5, 9, 0, 1, 2, 8, 6, 3] print(sorted(a)) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(a) #[7, 4, 5, 9, 0, 1, 2, 8, 6, 3] print(sorted(a, reverse = True)) #[9, 8, 7, 6, 5, 4, 3, 2, 1, 0] print(a.sort()) #None print(a) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] a.sort(reverse = True) print(a) #[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
反轉
reverse a = [7, 4, 5, 9, 0, 1, 2, 8, 6, 3] a.reverse() print(a) #[3, 6, 8, 2, 1, 0, 9, 5, 4, 7]