【问题标题】:How to merge two lists python如何合并两个列表python
【发布时间】:2015-10-15 19:45:57
【问题描述】:

我已经知道,如果我们有一个包含两个元组的列表,例如:

list = (('2', '23', '29', '26'), ('36', '0'))

通过以下命令:

new_list = list[0] + list[1]

会的;

list = ('2', '23', '29', '26', '36', '0')

如果下面有很多元组,我想使用循环命令之类的东西,我该怎么办?

list = [[list], [list2], [list3], ...]

我想要:

new_list = [list1, list2, list3,...]

【问题讨论】:

标签: python list merge


【解决方案1】:

使用itertools.chain,您可以简单地将列表作为参数提供,使用* 来扩展它们。

>>> from itertools import chain
>>> a_list = [[1], [2], [3]]
>>> list(chain(*a_list))
[1, 2, 3]
>>> tuple(chain(*a_list))
(1, 2, 3)

也不要使用预定义类型(例如 list)作为变量名,因为这会将它们重新定义为不是它们真正的样子,并且括号 (1, 2...) 语法会导致 tuple,而不是 @987654328 @。

【讨论】:

    【解决方案2】:

    首先,您没有像问题中所说的那样合并两个列表。 您正在做的是将 list of list 制作成 list

    有很多方法可以做到这一点。除了其他答案中列出的方式外,一种可能的解决方案可能是:

    for i in range(0, len(list_of_list)):
        item = list_of_list[i]
        for j in range(0,len(item)):
            new_list = new_list + [item]
    

    注意:此解决方案通常标记为 C - 类似,因为它不使用任何 Python 方法。

    【讨论】:

      【解决方案3】:
      >>> main_list = [[1,2,3],[4,5,6,7],[8,9]]
      >>> [item for sublist in main_list for item in sublist]
      [1, 2, 3, 4, 5, 6, 7, 8, 9]
      

      这使用嵌套列表理解方法。可以在here 找到如何阅读它们的一个很好的解释。

      想想你会如何使用常规循环来做到这一点。一个外部循环将提取一个列表,一个内部循环将列表的每个元素附加到结果中。

      >>> newlist = []
      >>> for sublist in main_list:
              for item in sublist:
                  newlist.append(item)
      
      >>> newlist
      [1, 2, 3, 4, 5, 6, 7, 8, 9]
      

      类似地,在上面的嵌套列表推导中 - for sublist in main_list 提取一个子列表,for item in sublist 循环遍历每个项目,推导开头的 item 对最终结果执行自动 list.append(item)。与常规循环的最大区别在于,您希望自动附加到最终结果的内容放在开头。

      【讨论】:

        【解决方案4】:

        使用 sum()

        >>> tp = ( ('2', '23', '29', '26'), ('36', '0'), ('4', '2') )
        >>> newtp = sum(tp, () )
        >>> newtp
        ('2', '23', '29', '26', '36', '0', '4', '2')
        

        itertools ,

        >>> from itertools import chain
        >>> tp = ( ('2', '23', '29', '26'), ('36', '0'), ('4', '2') )
        >>> newtp = tuple( chain(*tp) )
        >>> newtp
        ('2', '23', '29', '26', '36', '0', '4', '2')
        

        或理解,

        >>> tp = ( ('2', '23', '29', '26'), ('36', '0'), ('4', '2') )
        >>> newtp = tuple(i for subtp in tp for i in subtp)
        >>> newtp
        ('2', '23', '29', '26', '36', '0', '4', '2')
        

        【讨论】:

          【解决方案5】:

          一种简单的方法是使用reduce 内置方法。

          >>> list_vals = (('2', '23', '29', '26'), ('36', '0'))
          >>> reduce(lambda x, y: x + y, list_vals)
          ('2', '23', '29', '26', '36', '0')
          

          【讨论】:

            【解决方案6】:

            在这种情况下,列表的所有条目都是整数,因此很容易使用regular expressions。在这里使用正则表达式的额外好处是,它可以在任意嵌套列表上工作,而不是在列表为more than 1 degree nested 时不起作用的链。

            import re
            alist = [[1], [2],[3]]
            results = [int(i) for i in re.findall('\d+', (str(alist)))]
            print(results)
            

            输出是;

            >>> [1,2,4]
            

            因此,如果给定一个ugly 任意嵌套列表,例如:

            a_list = [[1], [2], [3], [1,2,3[2,4,4], [0]], [8,3]]
            

            我们可以的;

            a_list = [[1], [2], [3], [1,2,3, [2,4,4], [0]], [8,3]]
            results = [int(i) for i in re.findall('\d+', (str(a_list)))]
            print(results)
            

            输出是;

            >>> [1, 2, 3, 1, 2, 3, 2, 4, 4, 0, 8, 3]
            

            这可以说更有帮助。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-01-17
              • 2013-11-02
              • 2021-10-07
              • 2022-10-05
              相关资源
              最近更新 更多