【问题标题】:Python : How to add for each element of a list every element from another list?Python:如何为列表的每个元素添加另一个列表中的每个元素?
【发布时间】:2017-02-11 19:55:36
【问题描述】:

我真的是 Python 新手,我想管理一些我创建的列表。因此,在列表 A 中,我有年份的最后两位数字,在列表 B 中,我有从 000 到 999 的数字。

对于列表 A 中的每一年,我想使用循环添加列表 B 中的每个数字...

listA = ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99']

listB = ['000', '001', '002', '003', ...]

* 某事循环 *

listC = ['00000', '00001', '00002', ...]

谢谢!

【问题讨论】:

    标签: python list loops


    【解决方案1】:

    使用itertools.product。

    from itertools import product
    
    listA = ['00', '01', '02', '03']
    listB = ['000', '001', '002', '003']
    
    listC = [a + b for a, b in product(listB, listA)] # ['00000', '00001', '00002', ...]
    

    product 函数迭代提供的可迭代对象的笛卡尔积(所有组合的 k 元组从每个列表中抽取一个)。字符串连接用于连接元组的元素 (a + b)。列表推导将它们全部放入一个大列表中。

    【讨论】:

      【解决方案2】:

      你需要遍历listB,所以你会这样做:

      for itemB in listB:
      

      然后对于这些项目中的每一个,您需要将其与listA 中的每个元素匹配,因此您将在listA 上执行另一个嵌套循环。

      for itemA in listA:
      

      然后在两个循环中,您将拥有itemA 和itemB 的所有组合。因此,您只需将它们连接起来并将它们添加到输出列表中。

      完整解决方案

      ouptut = []
      for itemB in listB:
        for itemA in listA:
          output.append(itemB + itemA)
      

      itertools.product 如另一个解决方案中所述,可以为您提供更简洁的解决方案,因为它替换了两个 for 循环。

      【讨论】:

        【解决方案3】:

        虽然itertools.product 很方便,但一个简单的嵌套推导也可以做到:

        list_a = ['00', '01', '02', '03']  # Python conventions call for snake_case
        list_b = ['000', '001', '002', '003']
        
        list_c = [b + a for b in list_b for a in list_a]
        # ['00000', '00001', '00002', '00003', ... ]
        

        请注意,first for 的范围更宽,与自然英语相比,这有时似乎违反直觉。

        【讨论】:

          【解决方案4】:
          
          > Code to reproduce listA
          
          listA = []
          for i in range(0,10):
              for j in range(0,10):
                  e = str(i)+str(j)
                  listA.append(e)
          print(listA)
          
          > Code to reproduce listB
          
          
          listB = []
          for i in range(0,10):
              for j in range(0,10):
                  for k in range(0,10):
                      e = str(i)+str(j)+str(k)
                      listB.append(e)
          print(listB)
          
          > Now we just iterate over each element of listA and append one by one each element of listB, and keep appending the result to listC to get the final output list
          
          
          
          listC = []
          for i in listA:
              for j in listB:
          
                  listC.append(i+j)
          print(listC)
          

          【讨论】:

          • 欢迎来到 Stack Overflow。没有任何解释的代码转储很少有帮助。 Stack Overflow 是关于学习的,而不是提供 sn-ps 来盲目复制和粘贴。请edit您的问题并解释它如何比OP提供的更好。
          【解决方案5】:

          你可以使用内置函数 zip()

          例如:

          class SumList():
          
              def __init__(self, this_list):
                  self.mylist = this_list
          
              def __add__(self, other):
                  new_list = [ x + y for x,y in zip(self.mylist, other.mylist)]
                  return SumList(new_list)
          
              def __repr__(self):
                  return str(self.mylist)
          
          cc = SumList([1,2,3,4])
          dd = SumList([100,200,300,400])    
          ee = cc + dd
          print(ee)
          

          【讨论】:

          • 这不是 OP 想要的(因为两个列表的长度不相等,这一点非常清楚)。
          • 你到底为什么要把它封装在一个类中?
          猜你喜欢
          • 2021-01-21
          • 2017-03-05
          • 2023-03-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-04
          相关资源
          最近更新 更多