map() 对list列操作、行列转换

map() 对list的同一下标操作(对列操作)

读入的参数是(函数,list1,list2,list3)


>>>def  abc(a, b, c):

...            return  a*10000+b*100+c

>>> list1=[11,22,33]

>>> list2=[44,55,66]

>>> list3=[77,88,99]

>>>map (abc,list1,list2,list3)

[114477,225588,336699]

看到并行的效果了吧!在每个list中,取出了下标相同的元素,执行了abc()。


——————————————

>>> list1=[11,22,33]

>>>map(None,list1)

[11,22,33]

>>> list1=[11,22,33]

>>> list2=[44,55,66]

>>> list3=[77,88,99]

>>>map(None,list1,list2,list3)

[(11,44,77), (22,55,88), (33,66,99)]


行列转换了诶~~~~


等同zip……http://www.cnblogs.com/frydsh/archive/2012/07/10/2585370.html

x = [1, 2, 3]

y= [4, 5, 6]

z= [7, 8, 9]

xyz=zip(x, y, z)

print xyz

运行的结果是:

[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

——————————————————

基本用法:

Map函数:

原型:map(function, sequence),作用是将一个列表映射到另一个列表,

使用方法:

def f(x):

       return x**2

l = range(1,10)

map(f,l)

Out[3]: [1, 4, 9, 16, 25, 36, 49, 64, 81]

http://my.oschina.net/zyzzy/blog/115096


注意!python3的变化

>>> map(lambda x: x, [1,2,3])  

返回的就是一个map对象

>>> list(map(lambda x: x, [1,2,3]))

[1, 2, 3]

感覺Python3的用意是map用來處理大數據,所以返回 iteration 免得吃掉太多內存,一般情況下用list comprehension,或者直接for循環。

官方不推荐map生成list

http://segmentfault.com/a/1190000000322433

你可能感兴趣的:(map() 对list列操作、行列转换)