【问题标题】:Iterate a Python zip list and save the result in two different lists迭代 Python zip 列表并将结果保存在两个不同的列表中
【发布时间】:2020-04-26 20:32:06
【问题描述】:

我的程序有两个数字列表。我想要做的是迭代通过zip()获得的zip列表,对两个中间变量应用两个不同的函数,并将第一个结果(应用compute_add())保存在一个列表中,第二个(应用compute_sub())在另一个。我可以在 zip 列表上迭代两次,但我想知道是否有办法一次性完成。
这是我的代码,当然没有运行。

def compute_add(_x, _y):
    return _x * _y + 10


def compute_sub(_x, _y):
    return _x * _y - 10


list1 = [1, 2, 3, 4, 5]
list2 = [10, 20, 30, 40, 50]

res1, res2 = [compute_add(x, y), compute_sub(x, y) for x, y in zip(list1, list2)]

有什么建议吗?

【问题讨论】:

  • 使用单个 for 循环的唯一方法是不使用列表理解,因为您试图理解两个列表,而不是一个
  • 你的代码有什么问题?

标签: python list


【解决方案1】:

假设您想要一个列表中的总和而另一个列表中的差异,则没有一种直接的方法可以通过列表理解来做到这一点。您可以将这些对收集在一个元组中,然后分成两个列表:

tup = [(compute_add(x, y), compute_sub(x, y)) for x, y in zip(list1, list2)]
res1 = [t[0] for t in tup]
res2 = [t[1] for t in tup]

只需在 zip 上进行一个循环,我就可以这样做

res1 = []
res2 = []
for x, y in zip(list1, list2):
    res1.append(compute_add(x,y))
    res2.append(compute_sub(x,y))

如果函数真的这么简单,当然,你可以分解乘法:

for x, y in zip(list1, list2):
    mul = x * y
    res1.append(mul + 10)
    res2.append(mul - 10)

对于它的价值,What is the inverse function of zip in python? 有一个很好的答案“我怎么能做 zip() 的反向操作”,但它也有效地遍历列表两次。

【讨论】:

    【解决方案2】:

    方法一:使用list-comprehensions

    def compute_add(_x, _y):
        return _x * _y + 10
    def compute_sub(_x, _y):
        return _x * _y - 10
    
    list1 = [1, 2, 3, 4, 5]
    list2 = [10, 20, 30, 40, 50]
    res1 = [compute_add(x, y) for x, y in zip(list1, list2)]
    res2 = [compute_sub(x, y) for x, y in zip(list1, list2)]
    
    print(res1) # [20, 50, 100, 170, 260]
    print(res2) # [0, 30, 80, 150, 240]
    

    方法二:使用for循环

    您可以简单地遍历获得的 zip 列表并直接应用您定义的函数。见上面的代码。

    def compute_add(_x, _y):
        return _x * _y + 10
    def compute_sub(_x, _y):
        return _x * _y - 10
    
    list1 = [1, 2, 3, 4, 5]
    list2 = [10, 20, 30, 40, 50]
    res1 = []
    res2 = []
    for x, y in zip(list1, list2):
        res1.append(compute_add(x, y))
        res2.append(compute_sub(x, y))
    
    print(res1) # [20, 50, 100, 170, 260]
    print(res2) # [0, 30, 80, 150, 240]
    

    【讨论】:

      【解决方案3】:

      您可以在迭代期间将结果添加到两个列表中

      res1 = []
      res2 = []
      [[res1.append(compute_add(x, y)), res2.append(compute_sub(x, y))] for x, y in zip(list1, list2)]
      
      print(res1, res2) # [20, 50, 100, 170, 260] [0, 30, 80, 150, 240]
      

      【讨论】:

      • 不过,这有点滥用列表理解;您正在使用对副作用的理解,并丢弃它实际上所做的事情。
      【解决方案4】:

      只需像这样更改代码

      res1, res2 = [[compute_add(x, y) for x, y in zip(list1, list2)], [compute_sub(x, y) for x, y in zip(list1, list2)]]
      

      【讨论】:

        【解决方案5】:

        尝试使用zip

        res1, res2 = zip(*[(compute_add(x, y), compute_sub(x, y)) for x, y in zip(list1, list2)])
        

        【讨论】:

          猜你喜欢
          • 2020-08-05
          • 2016-08-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-07-22
          • 2021-12-16
          • 2019-11-28
          • 2014-02-15
          相关资源
          最近更新 更多