||
如果要将一个string list转换成int list (list里每个string都转成int),比如
['0','1','2'] -> [0,1,2]
[int(x) for x in list]
map(int, list)
list = [[0,1,2],[3,1,4]]
>>> list = [[0,1,2],[3,1,4]]
>>> [sum(x) for x in list]
[3, 8]
>>> map(sum,list)
[3, 8]
>>> list = [[0,1,2],[3,1,4]]
>>> zip(*list)
[(0, 3), (1, 1), (2, 4)]
>>> [sum(x) for x in zip(*list)]
[3, 2, 6]
>>> map(sum,zip(*list))
[3, 2, 6]
>>> x=[1,2,3]
>>> y=[4,5,6]
>>> zipped = zip(x,y)
>>> zipped
[(1, 4), (2, 5), (3, 6)]
>>> x2,y2=zip(*zipped)
>>> x2
(1, 2, 3)
>>> y2
(4, 5, 6)
>>> x3,y3=map(list,zip(*zipped))
>>> x3
[1, 2, 3]
>>> y3
[4, 5, 6]
第一个参数接收一个函数名,第二个参数接收一个可迭代对象
ls = [1,2,3] rs = map(str, ls) #打印结果 ['1', '2', '3'] lt = [1, 2, 3, 4, 5, 6] def add(num): return num + 1 rs = map(add, lt) print rs #[2,3,4,5,6,7] |
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-23 06:08
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社