小白的Python 学习笔记(一)List 常用方法汇总

一个小白的Python笔记

大家好,这是我在掘金的第一篇笔记,先简单介绍一下自己,我于2016年在北京工业大学本科毕业,同年9月来到法国巴黎Efrei工程师学校读研,在2018年11月毕业后开始工作,主要负责数据质量监测,分析,潜在风险预测,前端可视化报表制作等

这是我的基础简历,如果有想要交流Python或者招人的大佬,欢迎和我联系: 点击这里

工作需要使用Python做自动化及数据分析,我作为完全0基础的小白,成功忽悠HR通过面试后,深感会立刻露馅,于是一边工作一边在网上开始找教程进行学习,目前的我刚刚度过基础起步阶段,虽然水平不高,但是俗话说的好,无他,但手熟尔,只要多多练习,时间会是最好的证明,相信我们终有一天会成为高手,因此从这一系列开始,我会尽力总结学习过的基础知识,希望可以帮到那些同为小白的朋友们,当然,如果有大神发现文章中的错误一定要指出哈~

先皮一下,看个欧阳修的<<卖油翁>>:

陈康肃公尧咨善射,当世无双,公亦以此自矜。尝射于家圃,有卖油翁释担而立,睨之,久而不去。见其发矢十中八九,但微颔之。

康肃问曰:“汝亦知射乎?吾射不亦精乎?”翁曰:“无他,但手熟尔。”康肃忿然曰:“尔安敢轻吾射!”翁曰:“以我酌油知之。”乃取一葫芦置于地,以钱覆其口,徐以杓酌油沥之,自钱孔入,而钱不湿。因曰:“我亦无他,惟手熟尔。”康肃笑而遣之。

List的常用方法

这里我将会详细介绍一些我认为非常不错的List的使用方法,至于list 自带的一些基础用法,这里不再说明,感兴趣的朋友们可以看看我的基础教程: Python 基础起步 (五) 一定要知道的数据类型:初识ListPython 基础起步 (六) 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=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数据结构,简单好用!!!!!

转载于:https://juejin.im/post/5cfaa7cf51882542000628ec

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值