Python 進價之路 (一) List 進價方法彙總,新年快樂!
開啟變身模式
大家好, 從這一期開始,我們會從小白變身為中等小白,在基礎起步階段有太多的東西我沒有講到,但是俗話說的好,無他,但手熟爾,只要多多練習,時間會是最好的證明,相信我們終有一天會成為高手,因此從這一系列開始,讓我們一起更上一層樓,還是和往常一樣,我也是水平不高,如果有大神發現文章中的錯誤一定要指出哈~
先皮一下,看個歐陽修的<<賣油翁>>:
康肅問曰:“汝亦知射乎?吾射不亦精乎?”翁曰:“無他,但手熟爾。”康肅忿然曰:“爾安敢輕吾射!”翁曰:“以我酌油知之。”乃取一葫蘆置於地,以錢覆其口,徐以杓酌油瀝之,自錢孔入,而錢不溼。因曰:“我亦無他,惟手熟爾。 ”康肅笑而遣之。
List的進階用法
這裡我將會詳細介紹一些我認為非常不錯的List的使用方法,至於list 自帶的一些基礎用法,這裡不再說明,感興趣的朋友們可以看看我的基礎教程:Python 基礎起步 (五) 一定要知道的資料型別:初識List 和Python 基礎起步 (六) List的實用技巧大全 , 好啦,閒話少說,讓我們開始吧
把其他型別資料結構轉化為List型別
利用list(target) 即可實現把其他型別的資料結構轉化為List型別,十分實用,我們可以把字串,元組等資料結構轉化為List,也可以直接建立,像下圖一樣:
print(list())# 建立空List vowelString = 'aeiou'# 把字串轉化為List print(list(vowelString)) vowelTuple = ('a', 'e', 'i', 'o', 'u')# 從元組tuple轉化為List print(list(vowelTuple)) vowelList = ['a', 'e', 'i', 'o', 'u']# 從List到List print(list(vowelList)) vowelSet = {'a', 'e', 'i', 'o', 'u'}#從Set到List print(list(vowelSet)) Out: [] ['a', 'e', 'i', 'o', 'u'] ['a', 'e', 'i', 'o', 'u'] ['a', 'e', 'i', 'o', 'u'] ['a', 'e', 'i', 'o', 'u']
可能平時用的比較多的一般是從一個dict中提取 keys 和 values :
person = { 'name':'Peppa Pig', 'age':15, 'country':'bratian'} person_keys = list(person.keys()) person_values = list(person.values()) print(list(person))# 如果直接轉化一個字典只會把keys提取出來 print(list(person.keys())) print(list(person.values())) Out:['name', 'age', 'country'] ['name', 'age', 'country']# 把字典中的Keys提出 ['Peppa Pig', 15, 'bratian']# 把字典中的Values提出
這裡大家稍微注意下,如果直接用list(person)會預設等同於person.keys()
List排序方法彙總
總體來說,有兩種方法最為便捷,List.sort 或者sorted(List) ,具體來看如何實現,先看升序:
vowels = ['e', 'a', 'u', 'o', 'i'] vowels.sort() print('Sorted list Acending:', vowels)# 使用sort Out: Sorted list Acending: ['a', 'e', 'i', 'o', 'u']
vowels = ['e', 'a', 'u', 'o', 'i'] print('Sorted list Acending:', sorted(vowels))# 使用sorted Out:Sorted list Acending: ['a', 'e', 'i', 'o', 'u']
再來看降序:
vowels = ['e', 'a', 'u', 'o', 'i'] vowels.sort(reverse=True)# 使用sort print('Sorted list (in Descending):', vowels) Out: Sorted list (in Descending): ['u', 'o', 'i', 'e', 'a']
vowels = ['e', 'a', 'u', 'o', 'i'] print('Sorted list (in Descending):', sorted(vowels,reverse=True))# 使用sorted方法 Out:Sorted list (in Descending): ['u', 'o', 'i', 'e', 'a']
其實這裡裡面還有一個關鍵的引數是key,我們可以自定義一些規則排序,下面看個例子,用的是預設的len函式
,讓我們根據List中每個元素的長度來排序,首先是升序:
vowels = ['I', 'live', 'at', 'Paris', 'I','love','Python'] # 使用sort vowels.sort(key=len) print('Sorted by lenth Ascending:', vowels) Out:Sorted by lenth: ['I', 'I', 'at', 'live', 'love', 'Paris', 'Python']
vowels = ['I', 'live', 'at', 'Paris', 'I','love','Python'] # 使用 sorted print('Sorted by lenth Ascending:', sorted(vowels,key=len)) Out:Sorted by lenth Ascending: ['I', 'I', 'at', 'live', 'love', 'Paris', 'Python']
降序也差不多啦:
vowels = ['I', 'live', 'at', 'Paris', 'I','love','Python'] vowels.sort(key=len,reverse=True) print('Sorted by lenth Descending:', vowels) Out:Sorted by lenth Descending: ['Python', 'Paris', 'live', 'love', 'at', 'I', 'I']
vowels = ['I', 'live', 'at', 'Paris', 'I','love','Python'] print('Sorted by lenth Descending:', sorted(vowels,reverse=True)) Out:Sorted by lenth Descending: ['Python', 'Paris', 'live', 'love', 'at', 'I', 'I']
有關這個key我們可以自己定義,請看下面的大栗子:
def takeSecond(elem): return elem[1] random = [(2, 2), (3, 4), (4, 1), (1, 3)] random.sort(key=takeSecond) # sort list with key print('Sorted list:', random) Out: [(4, 1), (2, 2), (1, 3), (3, 4)]
這裡因為列表中的元素都是元組,所以我們規定按照元組的第二個數排序,所以結果如上,預設依然是reverse=Fause ,也就是升序,至於其他有意思的方法大家可以自己開發
利用Range生成List
Python裡面為我們提供了一個超好用的方法range(),具體的用法很多,不在這裡一一講解啦,但是大家可以看一下它如何和List結合在一起的:
five_numbers=list(range(5))#建立0~4的List print(five_numbers) odd_numbers=list(range(1,50,2))#建立0~50所有的奇數的List print(odd_numbers) even_numbers=list(range(0,50,2))#建立0~50所有的偶數的List print(even_numbers)
其實很簡單,用的最多的就是range(start, stop, hop)這個結構,其他的大家可以自行查詢哈
List列表推導式
其實這個東西無非就是為了減少程式碼書寫量,比較Pythonic的東西,基礎模板如下:
variable = [out_exp for out_exp in input_list if out_exp == 2]
大家直接看比較麻煩,還是直接上程式碼吧:
S = [x**2 for x in range(8)]# 0~7每個數的平方存為List V = [2**i for i in range(8)]# 2的 0~7次方 M = [x for x in S if x % 2 == 0]#找出在S裡面的偶數 print(S) print(V) print(M) Out:Square of 0 ~7:[0, 1, 4, 9, 16, 25, 36, 49] The x power of 2,(0<x<7) :[1, 2, 4, 8, 16, 32, 64, 128] The even numbers in S:[0, 4, 16, 36]
通過這個小栗子大家估計已經明白用法啦,推導式還可以這麼用:
verbs=['work','eat','sleep','sit'] verbs_with_ing=[v+'ing' for v in verbs]# 使一組動詞成為現在分詞 print(verbs_with_ing) Out:['working', 'eating', 'sleeping', 'siting']
或者加上查詢條件if :
all_numbers=list(range(-7,9)) numbers_positive = [n for n in all_numbers if n>0 ] numbers_negative = [n for n in all_numbers if n<0] devided_by_two = [n for n in all_numbers if n%2==0] print("The positive numbers between -7 to 9 are :",numbers_positive) print("The negative numbers between -7 to 9 are :",numbers_negative ) print("The numbers deveided by 2 without reminder are :",devided_by_two)``` Out:The positive numbers between -7 to 9 are : [1, 2, 3, 4, 5, 6, 7, 8] The negative numbers between -7 to 9 are : [-7, -6, -5, -4, -3, -2, -1] The numbers deveided by 2 without reminder are : [-6, -4, -2, 0, 2, 4, 6, 8]
總結
其實客觀來講,list在實際專案中所用的地方有限,特別是處理資料量較大的情況下,因為速度極慢,但是在一些邊邊角角的地方我個人用的還挺多,尤其是不求速度的地方,總的來講List是我個人比較喜歡的Python
資料結構,簡單好用!!!!!
好啦,我要去吃大餐啦,祝大家新年快樂呀!!!