工做中常常要处理各类各样的数据,遇到项目赶进度的时候本身写函数容易浪费时间。
Python 中有不少内置函数帮你提升工做效率!python
1.假设有一个数字列表 data, 过滤列表中的负数
列表推导式编程
result = [i for i in data if i >= 0]
filtersegmentfault
result = filter(lambda x: x>= 0, data)
2.学生的数学分数以字典形式存储,筛选其中分数大于 80 分的同窗微信
d = {x:randint(50, 100) for x in range(1, 21)}
{k: v for k, v in d.items() if v > 80}
使用 zip() 函数dom
zip() 函数用于将可迭代的对象做为参数,将对象中对应的元素打包成一个个元组,而后返回由这些元组组成的列表。
>>> s1 = {x: randint(1, 4) for x in sample('abfcdrg', randint(1,5))} >>> s1 {'b': 1, 'f': 4, 'g': 3, 'r': 1} >>> d = {k:v for k, v in zip(s1.values(), s1.keys())} >>> d {1: 'r', 4: 'f', 3: 'g'}
随机序列以下:函数
data = [randint(0,20) for _ in range(20)]
方法1: 能够使用字典来统计,以列表中的数据为键,以出现的次数为值spa
from random import randint def demo(): data = [randint(0, 20) for _ in range(30)] # 列表中出现数字出现的次数 d = dict.fromkeys(data, 0) for v in li: d[v] += 1 return d
方法2:直接使用 collections 模块下面的 Counter 对象code
>>> data = [randint(0, 20) for _ in range(30)] >>> data [7, 8, 5, 16, 10, 16, 8, 17, 11, 18, 11, 17, 15, 7, 2, 19, 5, 16, 17, 17, 12, 19, 9, 10, 0, 20, 11, 2, 11, 10] >>> c2 = Counter(data) >>> c2 Counter({17: 4, 11: 4, 16: 3, 10: 3, 7: 2, 8: 2, 5: 2, 2: 2, 19: 2, 18: 1, 15: 1, 12: 1, 9: 1, 0: 1, 20: 1}) >>> c2[14] 4 >>> c2.most_common(3) # 统计频度出现最高的3个数 [(17, 4), (11, 4), (16, 3)]
经过上面的练习,咱们知道能够用 Counter 来解决对象
import re from collections import Counter # 统计某个文章中英文单词的词频 with open('test.txt', 'r', encoding='utf-8')as f: d = f.read() total = re.split('\W+', d) # 全部的单词列表 result = Counter(total) print(result.most_common(10))
好比班级中学生的数学成绩以字典的形式存储:排序
{"Lnad": 88, "Jim", 71...}
请按数学成绩从高到底进行排序!
方法1: 利用 zip 将字典转化为元祖,再用 sorted 进行排序
>>> data = {x: randint(60, 100) for x in "xyzfafs"} >>> data {'x': 73, 'y': 69, 'z': 76, 'f': 61, 'a': 64, 's': 100} >>> sorted(data) ['a', 'f', 's', 'x', 'y', 'z'] >>> data = sorted(zip(data.values(), data.keys())) >>> data [(61, 'f'), (64, 'a'), (69, 'y'), (73, 'x'), (76, 'z'), (100, 's')]
方法2: 利用 sorted 函数的 key 参数
>>> data.items() >>> dict_items([('x', 64), ('y', 74), ('z', 66), ('f', 62), ('a', 80), ('s', 72)]) >>> sorted(data.items(), key=lambda x: x[1]) [('f', 62), ('x', 64), ('z', 66), ('s', 72), ('y', 74), ('a', 80)]
实际场景:在足球联赛中,统计每轮比赛都有进球的球员
第一轮: {"C罗": 1, "苏亚雷斯":2, "托雷斯": 1..}
第二轮: {"内马尔": 1, "梅西":2, "姆巴佩": 3..}
第三轮: {"姆巴佩": 2, "C罗":2, "内马尔": 1..}
模拟随机的进球球员和进球数
>>> s1 = {x: randint(1, 4) for x in sample('abfcdrg', randint(1,5))} >>> s1 {'d': 3, 'g': 2} >>> s2 = {x: randint(1, 4) for x in sample('abfcdrg', randint(1,5))} >>> s2 {'b': 4, 'g': 1, 'f': 1, 'r': 4, 'd': 3} >>> s3 = {x: randint(1, 4) for x in sample('abfcdrg', randint(1,5))} >>> s3 {'b': 4, 'r': 4, 'a': 2, 'g': 3, 'c': 4}
首先获取字典的 keys,而后取每轮比赛 key 的交集
因为比赛轮次数是不定的,因此使用 map 来批量操做
map(dict.keys, [s1, s2, s3])
而后一直累积取其交集, 使用 reduce 函数
reduce(lambda x,y: x & y, map(dict.keys, [s1, s2, s3]))
一行代码搞定!
了解更多内容,烦请关注本人公众号, Python编程与实战