Python3 zip,lambda,map,reduce的使用
2016-10-26 20:40
621 查看
使用Python进行数据处理的时候经常遇到一些Python独有的小函数,使用这些小函数
可以大大减少代码量。Python2中的使用方法与3有些不同,具体参见
这里写链接内容
可以大大减少代码量。Python2中的使用方法与3有些不同,具体参见
这里写链接内容
# -*- coding: utf-8 -*- """ Created on Wed Oct 26 11:51:32 2016 @author: Jian """ import functools a = [1,2,3,4,5] b = [2,2,9,0,9] def pick_the_largest(a, b): result = [] # A list of the largest values # Assume both lists are the same length list_length = len(a) for i in range(list_length): result.append(max(a[i], b[i])) return result print(pick_the_largest(a, b)) """ This function takes two equal-length collections, and merges them together in pairs. If we use this on our list of values, we get the following: [2, 2, 9, 4, 9] """ print(list(zip(a,b))) """ function 参数:返回值 """ f = lambda x, y : x + y print(f(1,1)) """ map(func,seq) func是计算的函数,seq是传入的数组 """ def fahrenheit(T): return ((float(9)/5)*T + 32) def celsius(T): return (float(5)/9)*(T-32) temperatures = (36.5, 37, 37.5, 38, 39) F = map(fahrenheit, temperatures) C = map(celsius, F) temperatures_in_Fahrenheit = list(map(fahrenheit, temperatures)) temperatures_in_Celsius = list(map(celsius, temperatures_in_Fahrenheit)) print(temperatures_in_Fahrenheit) print(temperatures_in_Celsius) """ map lambda 配合使用效果更好 """ C = [39.2, 36.5, 37.3, 38, 37.8] F = list(map(lambda x: (float(9)/5)*x + 32, C)) print(F) """ lambda 写计算函数,a,b传入两个数组参数 [18, 14, 14, 14] """ a = [1,2,3,4] b = [17,12,11,10] c = [-1,-4,5,9] print(list(map(lambda x,y:x+y, a,b))) """ filter(function, sequence) 返回True的数进数组 """ fibonacci = [0,1,1,2,3,5,8,13,21,34,55] odd_numbers = list(filter(lambda x: x % 2, fibonacci)) print(odd_numbers) """ 113 102 """ print(functools.reduce(lambda x,y: x+y, [47,11,42,13])) #累加 from functools import reduce #找到最大值 f = lambda a,b: a if (a > b) else b print(reduce(f, [47,11,42,102,13])) #累乘 print(reduce(lambda x, y: x*y, range(1,3))) m = list(range(1,3)) #[1, 2] print(m)
相关文章推荐
- Python 中的 map, reduce, zip, filter, lambda基本使用方法
- python中的zip、map、reduce 、lambda函数的使用。
- Python碎片化知识:map,reduce,lambda以及zip
- python 的几个内置函数(lambda ,zip, filter, map, reduce )用法
- Python常用内置函数整理(lambda,reduce,zip,filter,map)
- python几个重要的函数(lambda,filter,reduce,map,zip)
- Python高阶函数(Map、Reduce、Filter)和lambda函数一起使用 ,三剑客
- 【python】详解lambda匿名函数以及结合map、reduce、filter、sorted等使用
- Python list--> Filter , Map , Reduce 使用笔记 (lambda )
- Python学习笔记(5):lambda,filter,map,reduce,zip用法整理
- Python中filter、lambda、map、reduce的使用
- python几个重要的函数(lambda,filter,reduce,map,zip) 一、匿名函数lambda lambda argument1,argument2,...argumentN :e
- 转 python 的几个内置函数(lambda ,zip, filter, map, reduce )用法
- python 的几个内置函数(lambda ,zip, filter, map, reduce )用法
- Python经常使用内置函数介绍【filter,map,reduce,apply,zip】
- Python特殊语法:filter、map、reduce、lambda
- python中的函数:filter/map/reduce/lambda
- [Java 8] (10) 使用Lambda完成函数组合,Map-Reduce以及并行化
- Python常用内置函数介绍【filter,map,reduce,apply,zip】
- Python filter、map、reduce、lambda