python办公自动化

大师兄的Python学习笔记(十二): 常用高级函数

2020-02-16  本文已影响0人  superkmi

大师兄的Python学习笔记(十一): 时间模块time,datetime和calendar
大师兄的Python学习笔记(十三): 理解装饰器

一、lambda匿名函数

>>># 普通函数实现
>>>def sum(x,y):
>>>    return x+y
>>>print(sum(2,3))
5

>>># 匿名函数实现
>>>sum = lambda x,y:x+y
>>>print(sum(2,3))
5

二、zip(<iterable1>,<iterable2>...)函数

>>>l1 = ['武汉','北京','广州','上海']
>>>l2 = [54406,375,2194,326]
>>>l3 = [2420,3,8] #这里的list长度为3,所以zip后的数据以最短的列表为准
>>>z = zip(l1,l2,l3)
>>>print([x for x in z])
[('武汉', 54406, 2420), ('北京', 375, 3), ('广州', 2194, 8)]

三、map(<function>,<iterable>)函数

>>>def square(d):
>>>    return d**2

>>>l = [i for i in range(10)]
>>>m = map(square,l)
>>>print([x for x in m])
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
l = [i for i in range(10)]
m = map(lambda d:d**2,l)
print([x for x in m])
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

四、filter(<function>,<iterable>)函数

>>>def is_odd(d):
>>>    if d%2==1:
>>>        return d

>>>l = [i for i in range(10)]
>>>f = filter(is_odd,l)
>>>print([x for x in f])
[1, 3, 5, 7, 9]
>>>l = [i for i in range(10)]
>>>m = filter(lambda d: d % 2 == 1 and d or None,l) # 三项判断式
>>>print([x for x in m])
[1, 3, 5, 7, 9]

五、reduce(<function>,<iterable>,<initializer=0>)函数

>>>from functools import reduce
>>>def sum(x,y):
>>>    return x+y

>>>l = [i for i in range(100)]
>>>s = reduce(sum,l)
>>>print(s)
4950
>>>from functools import reduce
>>>l = [i for i in range(100)]
>>>s = reduce(lambda x,y:x+y,l)
>>>print(s)
4950

六、enumerate(<iterator>,<start=0>)函数

>>>cities = ['北京','上海','天津','重庆']
>>>cities_with_serial = enumerate(city,101)
>>>print([x for x in cities_with_serial])
[(101, '北京'), (102, '上海'), (103, '天津'), (104, '重庆')]

七、collections模块

1. namedtuple(<name>,<value1>,<value2>,...)
>>>from collections import namedtuple
>>># 定义数据类型
>>>Point = namedtuple('Point',['x','y']) # 坐标

>>># 实例化
>>>beijing_point = Point(100,200) 
>>>shanghai_point = Point(200,300)
>>>print('北京的坐标是{}'.format(beijing_point))
>>>print('上海的坐标是{}'.format(shanghai_point))
>>>print('北京的x坐标是{}'.format(beijing_point.x))
京的坐标是Point(x=100, y=200)
上海的坐标是Point(x=200, y=300)
北京的x坐标是100
2. deque(<iterable>)
>>>from collections import deque
>>># 实例化数据类型
>>>cities = ['北京','上海','天津','重庆']
>>>cities_dq = deque(cities)
>>>print(cities_dq)
deque(['北京', '上海', '天津', '重庆'])

>>># 列表操作
>>>selected = cities_dq.popleft() # 返回并删除左边第一个元素‘北京’
>>>cities_dq.appendleft('武汉') # 从左边添加一个元素'武汉'
>>>print(selected)
北京
>>>print(cities_dq)
deque(['武汉', '上海', '天津', '重庆'])
3. defaultdict(<default>)
>>>from collections import defaultdict
>>># 设置默认值
>>>cities_count = defaultdict(lambda:'数据统计中')
>>># 数据操作
>>>cities_count['北京'] = 325
>>>print(cities_count['北京'])
325
>>>print(cities_count['上海'])
数据统计中
4. OrderedDict
>>># 普通dict
>>>cities = dict()
>>>cities['北京'] = 375
>>>cities['上海'] = 326
>>>cities['武汉'] = 54406
>>>cities['广州'] = 2194
>>>print(cities) # dict是没有顺序的
{ '上海': 326, '武汉': 54406, '北京': 375,'广州': 2194}

>>># OrderedDict
>>>from collections import OrderedDict
>>>cities_od = OrderedDict()
>>>cities_od['北京'] = 375
>>>cities_od['上海'] = 326
>>>cities_od['武汉'] = 54406
>>>cities_od['广州'] = 2194
>>>print(cities_od)
OrderedDict([('北京', 375), ('上海', 326), ('武汉', 54406), ('广州', 2194)])
5. Counter
>>>import collections,random
>>># 生成一个list包含若干元素
>>>l = list()
>>>for i in range(100):
>>>    l.append(random.choice(['猫','狗','兔子','熊']))

>>># 计算每种元素的个数
>>>counter = collections.Counter()
>>>for i in l:
>>>    counter[i]+=1
>>>print(counter)
>Counter({'狗': 37, '猫': 26, '兔子': 20, '熊': 17})

参考资料



本文作者:大师兄(superkmi)

上一篇下一篇

猜你喜欢

热点阅读