赞
踩
求阶乘:reduce(lambda x,y:x*y, range(1, 101))
求和:sum(range(101))
合并字典:dict1.update(dict2)
去重:[x for x in set(listx)]
排序:listx.sort() | sorted(listx)
统计:collections.Counter(listx) | listx.count(argv)
zip(list1, list2, ......)
enumerate(listx)
dictx.items()、dictx.keys()、dictx.values()
format传参:
str = "helle, {1}, let's to {2}"``.format``(parm1, parm2)
str = f"helle, {parm1}, let's to {parm2}"
map(func, listx)
:listx中的值依次传入func,返回一个新的序列。eg:list(map(lambda x:x*x, range(1,10))
filter(func, listx)
:listx中的值依次传入func,留下返回true的,构成一个新的序列。eg:list(filter(lambda x:x%2==0, range(100)))
reduce(func, listx)
:func接收两个参数,listx中的值依次传入func,会对func的结果进行累积,作为下一次调用的一个参数,一般会以listx中的前两个值作为初始参数调用func,返回一个最终值.
eg:reduce(lambda x,y:x*y, range(1, n+1))、 reduce(lambda x,y:x+y, range(10))
注:python3已不支持,需要from functools import reduce
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。