【问题标题】:How to iterate over two arrays in parallel?如何并行迭代两个数组?
【发布时间】:2016-04-19 17:31:41
【问题描述】:

我看起来能够并行迭代两个数组(或只有一个 for 循环)。

这是我尝试过的脚本...

#!/usr/bin/env python

list1 = [ 'one', 'two', 'three' ]
list2 = [ 'I', 'II', 'III', 'IV', 'V' ]

for word in list1:
    print word + " from list1"

for roman in list2:
    print roman + " from list2"

for ( word, roman ) in (list1 list2):
         if word:
                 print word + " from list1"

    if roman:
        print roman + " from list2"

但显然不正确,因为我收到语法错误:

  File "./twoarr.py", line 12
    for ( word, roman ) in (list1 list2):
                                      ^
SyntaxError: invalid syntax

我正在尝试获得如下所示的输出:

one from list1
I from list2
two from list1
II from list2
three from list1
III from list2
IV from list2
V from list2

【问题讨论】:

  • 我建议更改这个问题的标题。在我看来,您不希望并行进行。你想在一个循环中完成它。标题可能暗示了不同的东西。
  • 也许是这样。但对我来说,并行并不总是意味着多任务处理(分叉、子父进程等)。我的意思是“并行”,而不是串行处理数组(一个接一个)。

标签: python


【解决方案1】:

您可以使用zip 遍历 2 个列表。

>>> list1 = [ 'one', 'two', 'three' ]
>>> list2 = [ 'I', 'II', 'III', 'IV', 'V' ]
>>> for (x, y) in zip(list1, list2):
...     print x + " from list1"
...     print y + " from list2"
... 
one from list1
I from list2
two from list1
II from list2
three from list1
III from list2

注意zip 将提供直到列表很小。所以在你的情况下,list1 有 3 个元素,list2 有 5 个元素,所以 zip 将只提供 3 个元素的数据。您可以使用izip_longest 到达list2 中的所有元素

【讨论】:

【解决方案2】:

要遍历多个列表,您可以使用内置函数zip。根据Documentation,此函数返回一个元组列表,其中第 i 个元组包含来自每个参数序列或可迭代对象的第 i 个元素。因此,应用于您的特定示例

list1 = [ 'one', 'two', 'three' ]
list2 = [ 'I', 'II', 'III', 'IV', 'V' ]

for word in list1:
    print word + " from list1"

for roman in list2:
    print roman + " from list2"

for word, roman in zip(list1 list2):
    print word + " from list1"
    print roman + " from list2"

zip 的唯一缺点是,当您的列表(如本例所示)长度不相等时,zip 将返回一个元组列表,每个元组的维度都等于较小的一个。要支持最长的,并在必要时填写 None,只需将 zip 替换为 itertools.izip_longest

from itertools import izip_longest

list1 = [ 'one', 'two', 'three' ]
list2 = [ 'I', 'II', 'III', 'IV', 'V' ]

for word in list1:
    print word + " from list1"

for roman in list2:
    print roman + " from list2"

for word, roman in izip_longest(list1, list2):
    print word + " from list1"
    print roman + " from list2"

【讨论】:

  • 谢谢!这很好用。只是挑剔的一点。您在 izip_longest() 调用中省略了“,”。我用for word, roman in izip_longest(list1, list2):
  • 是的,是一个错字。我刚刚修好了。
【解决方案3】:

我有类似的要求,由

实现
for i in range(len(list1)):
    print list1[i], list2[i]

如果列表的长度不同,则需要更多检测,或者使用一些 try/except 语句来避免无效索引

【讨论】:

    【解决方案4】:

    如果您希望它不仅混合两个列表,而且真正并行运行,请使用两个线程:

    import _thread
    
    def print_list(name, list):
        for word in list:
            print(word + " from " + name + "\r\n");
    
    list1 = [ 'one', 'two', 'three' ]
    list2 = [ 'I', 'II', 'III', 'IV', 'V' ]
    
    _thread.start_new_thread( print_list, ("list1",list1) )
    _thread.start_new_thread( print_list, ("list2",list2) )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-25
      • 1970-01-01
      • 2012-04-25
      • 2014-11-08
      • 2012-09-28
      相关资源
      最近更新 更多