你们好, 从这一期开始,咱们会从小白变身为中等小白,在基础起步阶段有太多的东西我没有讲到,可是俗话说的好,无他,但手熟尔,只要多多练习,时间会是最好的证实,相信咱们终有一天会成为高手,所以从这一系列开始,让咱们一块儿更上一层楼,仍是和往常同样,我也是水平不高,若是有大神发现文章中的错误必定要指出哈~segmentfault
先皮一下,看个欧阳修的<<卖油翁>>:微信
陈康肃公尧咨善射,当世无双,公亦以此自矜。尝射于家圃,有卖油翁释担而立,睨之,久而不去。见其发矢十中八九,但微颔之。康肃问曰:“汝亦知射乎?吾射不亦精乎?”翁曰:“无他,但手熟尔。”康肃忿然曰:“尔安敢轻吾射!”翁曰:“以我酌油知之。”乃取一葫芦置于地,以钱覆其口,徐以杓酌油沥之,自钱孔入,而钱不湿。因曰:“我亦无他,唯手熟尔。”康肃笑而遣之。数据结构
这里我将会详细介绍一些我认为很是不错的List的使用方法,至于list 自带的一些基础用法,这里再也不说明,感兴趣的朋友们能够看看个人基础教程: Python 基础起步 (五) 必定要知道的数据类型:初识List 和 Python 基础起步 (六) List的实用技巧大全, 好啦,闲话少说,让咱们开始吧dom
把其余类型数据结构转化为List类型
利用list(target)便可实现把其余类型的数据结构转化为List类型,十分实用,咱们能够把字符串,元组等数据结构转化为List,也能够直接建立,像下图同样:ide
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()spa
List排序方法汇总
整体来讲,有两种方法最为便捷,List.sort 或者 sorted(List),具体来看如何实现,先看升序:code
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=False ,也就是升序,至于其余有意思的方法你们能够本身开发
逆序输出List
若是咱们想要逆序输出一个List,简单来讲有两种方法比较好用,切片和reverse,来看例子:
vowels = ['I', 'live', 'at', 'Paris', 'I','love','Python'] print(list(reversed(vowels))) print(list[::-1]) Out:['Python', 'love', 'I', 'Paris', 'at', 'live', 'I'] ['Python', 'love', 'I', 'Paris', 'at', 'live', 'I']
利用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
数据结构,简单好用!!!!!
若是你们对List的其余使用技巧感兴趣,能够关注个人微信公众号Python极简教程,我会把最高效,简洁的小技巧一一记录下来,分享给你们: