Python List 基礎學習

流水無情88發表於2017-03-16

list&tuple&dict

list

list 常見操作

初始化:
list1 = [123, 'abc', 4.56, ['inner', 'list'], 7-9j]
list2 = [None, 'Something to see here']
emptylist=[]

list 位置 or index

list1 = [123, 'abc', 4.56, ['inner', 'list'], 7-9j]
正向算0 1 2 3 4, 反向過來的位置就是 -1 -2 -3 -4 -5 [反向是從-1開始算起的喲!]

list 切片:
tips: list1[start?pace] 索引從0開始計數,包括start,不包括end!

list1[1:4]  #['abc', 4.56, ['inner', 'list']]  
list1[3][0] #結果是'inner'  類似於二維陣列。
list1[:2] 等價於 list1[0:2:1]
list1[3:5]=['abc','xyz',345] #切片可以被賦值,元素個數可以不相同。

其他操作

list1[2]='this is list1[2]!'    #修改list 的某個值
list1.append("append element") #append 新增元素
del list1[2]

list1.remove(123)   #刪除第一個遇到的 123 值。如果沒有這個元素會報錯:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list

list1.pop[index] #index不指定,預設是是list 最後一個元素。 ***和 remove 的區別是remove指定的是值,pop指定的是位置***
del list1   #刪除整個list1

多個 list 之間的操作

list3 = ['abc', 999]
list2 = ['xyz', 789]
list2 < list3 False
兩個 list 之間比較,從第0個開始比較,一旦比較出了結果就不繼續後續元素的比較了。
不同型別的比較,例如 (123 < 'xyz') == True  #解釋:python 核心程式設計P140說到:如果有一方的元素是數字,另一方最大[數字最小]

list * 2  *變成了重複操作符。

成員關係操作

list1 = [['hsp', 'wish'], 'abc', 4.56]
>>> ['hsp', 'wish'] in list1
True
>>> 'dba' in list1
False

常見函式
這裡指的是 Python 直譯器支援的內建函式

>>> list1
[['hsp', 'wish'], 'abc', 4.56]
>>>len(list1)
>>> max(list1),min(list1)
('abc', 4.56)
>>> list1.reverse()

enumerate 列舉元素
>>> list1
[4.56, 'abc', ['hsp', 'wish']]
>>> list(enumerate(list1))
[(0, 4.56), (1, 'abc'), (2, ['hsp', 'wish'])]
>>> for i,value in enumerate(list1):
...   print i,value
...
0 4.56
1 abc
2 ['hsp', 'wish']


zip 感覺把兩個list整合在一起了,後續看下為啥??
>>> list1
[4.56, 'abc', ['hsp', 'wish']]
>>> list2
['xyz', 789]
>>> zip(list1,list2)
[(4.56, 'xyz'), ('abc', 789)]

sum     注意:list 元素需要是數字型別
>>> a = [6, 4, 5]
>>> sum(a)
15

tuple() && list()          
tuple 和 list 之間的互換。
>>> list2
['xyz', 789]
>>> tuple2= tuple(list2)
>>> tuple2
('xyz', 789)
>>> list(tuple2)
['xyz', 789]

列表型別的內建函式 [類似於Java 的 class 的 method]

dir(list) 可以檢視列表型別的內建函式。
'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'


>>> names
['wfz', 'moon', 'er0', 'info', 0, 1]
>>> names.append(range(2))            #list.append(x)
>>> names
['wfz', 'moon', 'er0', 'info', 0, 1, [0, 1]]
>>> names.pop()                 #list.pop([i]) Remove the item at the given position in the list, and return it. 預設是末尾元素。
[0, 1]
>>> names
['wfz', 'moon', 'er0', 'info', 0, 1]
>>> names.index('info')    #list.index(x)  Return the index in the list of the first item whose value is x. It is an error if there is no such item.
3
>>> names.remove(0)     #移除值包含0的元素,list.remove(x)
Remove the first item from the list whose value is x.
>>> names
['wfz', 'moon', 'er0', 'info', 1]
>>> names.sort()            #sort(func=None,key=None,reverse=False) 預設的reverse 正序排序。
>>> names
[1, 'er0', 'info', 'moon', 'wfz']
>>> names.reverse()     #list.reverse()  Reverse the elements of the list, in place.
>>> names
['wfz', 'moon', 'info', 'er0', 1]
>>> names.insert(2,'th000') # list.insert(i, x)  Insert an item at a given position.  insert 和 append 的區別是可以選擇位置。
>>> names.extend(range (2))   #list.extend(L)  L 可以是一個list
>>> names
['wfz', 'moon', 'th000', 'info', 'er0', 1, 0, 1]
>>> names.count(1)  #list.count(x) Return the number of times x appears in the list.
2


判斷一個元素在list 的索引位置中,先 in 判斷下,再呼叫 index 方法
>>> list1
[4.56, 'abc', ['hsp', 'wish'], 'abc']
>>> 'abc' in list1
True
>>> list1.index('abc')
1           #注意只會返回一個位置

sorted 和 sort 區別

sorted內建函式、返回list 排序後得值,對list 的本身的值沒有修改。list.sort() 是 list 本身的方法 [dir(list) 檢視],會對 list 本身修改掉,不返回值。

>>> list2
['xyz', 789]
>>> sorted(list2)
[789, 'xyz']
>>> list2
['xyz', 789]
>>> list2.sort()
>>> list2
[789, 'xyz']

用list實現佇列

Stack實現
Stack.put('A') == list1.append('A')
Stack.pop() == list1.pop()

Queue實現
Queque.enque('A') == list1.append('A')
Queque.deque('A') == list1.pop(0)

相關文章