相关疑难解决方法(0)

如何并行迭代两个列表?

我在Python中有两个迭代,我想成对地遍历它们:

foo = (1, 2, 3)
bar = (4, 5, 6)

for (f, b) in some_iterator(foo, bar):
    print "f: ", f, "; b: ", b
Run Code Online (Sandbox Code Playgroud)

它应该导致:

f: 1; b: 4
f: 2; b: 5
f: 3; b: 6
Run Code Online (Sandbox Code Playgroud)

一种方法是迭代索引:

for i in xrange(len(foo)):
    print "f: ", foo[i], "; b: ", b[i]
Run Code Online (Sandbox Code Playgroud)

但这对我来说似乎有点不合时宜.有没有更好的方法呢?

python iterator for-loop list

767
推荐指数
6
解决办法
58万
查看次数

如何将列表合并到元组列表中?

什么是Pythonic方法来实现以下目标?

# Original lists:

list_a = [1, 2, 3, 4]
list_b = [5, 6, 7, 8]

# List of tuples from 'list_a' and 'list_b':

list_c = [(1,5), (2,6), (3,7), (4,8)]
Run Code Online (Sandbox Code Playgroud)

每个成员list_c都是一个元组,其第一个成员来自list_a,而第二个来自list_b.

python merge tuples list

279
推荐指数
7
解决办法
23万
查看次数

3
推荐指数
2
解决办法
2928
查看次数

标签 统计

list ×3

python ×3

for-loop ×1

iterator ×1

merge ×1

tuples ×1