Python zip(), map(), filter(), reduce()
2022/7/25 1:52:47
本文主要是介绍Python zip(), map(), filter(), reduce(),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
- zip
a = [1,2,3] b = ['a','b','c'] c = [1,2,3] d = ['a','b','c'] print(dict(zip(a,b))) #{1: 'a', 2: 'b', 3: 'c'} print(list(zip(a,b))) # [(1, 'a'), (2, 'b'), (3, 'c')]
- filter
filter(function or None, iterable) 遍历可迭代对象,过滤符合要求的值
c = [-10, 28, 9, -5, 30, 5] print(list(filter(lambda a:a>0, c)))
- map
map(function or None, iterable) 遍历可迭代对象,每个值作用到函数,返回一个列表
c = [-10, 28, 9, -5, 30, 5] print(list(map(lambda a:a>0, c)))
- reduce
educe() 函数表示对参数序列中所有元素进行累积
print (reduce(lambda x,y:x+y, range(1, 101))) # 计算1-100的值
5.map和filter的却别
def only_int(x): try: if isinstance(x, int): return True else: return False except ValueError as e: return False dt1 = filter(only_int, [1, 2, 3, 3, '3232', -34.5, 34.5]) print(list(dt1)) #[1, 2, 3, 3] dt2 = map(only_int, [1, 2, 3, 3, '3232', -34.5, 34.5]) print(list(dt2)) # [True, True, True, True, False, False, False]
这篇关于Python zip(), map(), filter(), reduce()的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python