python-zip函数使用

zip((1,2,3),(10,20,30),(100,200,300))
[(1, 10, 100), (2, 20, 200), (3, 30, 300)]

[sum(x) for x in zip((1,2,3),(10,20,30),(100,200,300))]
[111, 222, 333]
To do this with an arbitrarily large set of tuples:

myTuples = [(1,2,3), (10,20,30), (100,200,300)]
[sum(x) for x in zip(*myTuples)]
[111, 222, 333]
sidenote: in python3, note that zip returns a lazy iterable, which you can always explicitly turn into a list like any other kind of iterable: list(zip(…))

猜你喜欢

转载自www.cnblogs.com/LearnFromNow/p/9345421.html
今日推荐