【问题标题】:How to merge item in list如何合并列表中的项目
【发布时间】:2014-05-28 08:55:38
【问题描述】:

我在 python 中的列表有问题。当我打印“列表”时,我得到了这个结果:

[1,2,3]
[4,5,6]

所以我猜我在一个变量中有两个列表。如何将这些项目合并到一个变量中?

【问题讨论】:

  • 您能否提供更多代码以清楚起见
  • 一个变量(或python中的“名称”),一次只能是一个对象。您拥有的是一个包含两个列表的列表(或类似列表),分别包含 1、2、3 和 4、5、6。

标签: python list python-2.7 merge


【解决方案1】:

如果:

returnList = [[1,2,3], [4,5,6]]

你可以很容易做到:

>>> returnList = [j for i in returnList for j in i]

或者:

>>> returnList = [j for i in returnList for j in i]

或者,您也可以这样做:

>>> returnList = reduce(lambda i,j: i+j, returnList)

两者都会返回:[1, 2, 3, 4, 5, 6]

【讨论】:

  • 好的,但是我必须用我的变量 returnList 替换什么?
  • @user3531797, a 是你替换的
【解决方案2】:

我会这样做,使用itertools.chain:

>>> import itertools
>>> a = [[1, 2, 3], [4 , 5, 6]]
>>> a = itertools.chain.from_iterable(a)
>>> a
[1, 2, 3, 4, 5, 6]

【讨论】:

    【解决方案3】:

    试试这个,

    >>> a
    [[1, 2, 3], [4, 5, 6]]
    >>> result=[]
    >>> for i in a:
        result+=i
    
    
    >>> result
    [1, 2, 3, 4, 5, 6]
    >>>
    

    >>> a
    [[1, 2, 3], [4, 5, 6]]
    >>> sum(a, [])
    

    输出:

    [1, 2, 3, 4, 5, 6]
    

    >>> a1
    [1, 2, 3]
    >>> a2
    [4, 5, 6]
    >>> [item for item in itertools.chain(a1, a2)]
    

    输出:

    [1, 2, 3, 4, 5, 6]
    

    【讨论】:

    • 这可以简化为result = sum(a, [])
    • @AlexThornton 谢谢,已更新。
    • 虽然重要的是要注意这可能相当慢。
    【解决方案4】:
    l = [['0', '1', '2'], ['3', '4', '5']]
    
    def merge_list(l):
        temp = []
        for i in l:
            for j in i:
                temp.append(j)
        return temp
    
    print merge_list(l)
    

    结果

    ['0', '1', '2', '3', '4', '5']
    

    【讨论】:

    • 输入和结果一样吗?
    • 是的,甚至没有注意到...返回输入而不是温度...哎呀。感谢您的提醒
    【解决方案5】:

    Merge 2 list in python

    listtwo = [8,9,0]
    listone = [1,2,3,4,5]
    mergedlist = listone + listtwo
    

    【讨论】:

      【解决方案6】:

      这里我收集了一些解决方法,试试timeit:

      这是我的 sn-p:

      #!/usr/bin/python
      
      def f1(List):
          x = []
          [ x.extend(y) for y in List ]
          return x
      
      def f2(List):
          return sum(List, [])
      
      def f3(List):
          temp = []
          for i in List:
              for j in i:
                  temp.append(j)
          return temp
      
      def f4(List):
          result=[]
          for i in List:
              result += i
          return result
      
      def f5(List):
          return [j for i in List for j in i]
      
      import cProfile
      import itertools
      from faker import Faker
      from timeit import Timer
      
      s = Faker()
      # instead of faker you can use random module
      func = [ f1, f2, f3, f4, f5, f6 ]
      Lis = [[ s.random_int(min=1, max=99)
              for x in range(1000) ]
              for x in range(100)]
      
      for fun in func:
          t = Timer(lambda: fun(Lis))
          print fun.__name__, cProfile.run('t.timeit(number=1000)')
      

      输出:

      f1          102011 function calls in 0.701 seconds
      
         Ordered by: standard name
      
         ncalls  tottime  percall  cumtime  percall filename:lineno(function)
              1    0.000    0.000    0.701    0.701 <string>:1(<module>)
           1000    0.040    0.000    0.495    0.000 merge.py:3(f1)
           1000    0.001    0.000    0.495    0.000 merge.py:42(<lambda>)
              1    0.000    0.000    0.000    0.000 timeit.py:143(setup)
              1    0.000    0.000    0.701    0.701 timeit.py:178(timeit)
              1    0.206    0.206    0.701    0.701 timeit.py:96(inner)
              1    0.000    0.000    0.000    0.000 {gc.disable}
              1    0.000    0.000    0.000    0.000 {gc.enable}
              1    0.000    0.000    0.000    0.000 {gc.isenabled}
              1    0.000    0.000    0.000    0.000 {globals}
              1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
         100000    0.455    0.000    0.455    0.000 {method 'extend' of 'list' objects}
              2    0.000    0.000    0.000    0.000 {time.time}
      
      
      None
      f2          3011 function calls in 37.747 seconds
      
         Ordered by: standard name
      
         ncalls  tottime  percall  cumtime  percall filename:lineno(function)
              1    0.000    0.000   37.747   37.747 <string>:1(<module>)
           1000    0.003    0.000   37.418    0.037 merge.py:42(<lambda>)
           1000    0.004    0.000   37.415    0.037 merge.py:8(f2)
              1    0.000    0.000    0.000    0.000 timeit.py:143(setup)
              1    0.000    0.000   37.747   37.747 timeit.py:178(timeit)
              1    0.329    0.329   37.747   37.747 timeit.py:96(inner)
              1    0.000    0.000    0.000    0.000 {gc.disable}
              1    0.000    0.000    0.000    0.000 {gc.enable}
              1    0.000    0.000    0.000    0.000 {gc.isenabled}
              1    0.000    0.000    0.000    0.000 {globals}
              1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
           1000   37.411    0.037   37.411    0.037 {sum}
              2    0.000    0.000    0.000    0.000 {time.time}
      
      
      None
      f3          100002011 function calls in 28.044 seconds
      
         Ordered by: standard name
      
         ncalls  tottime  percall  cumtime  percall filename:lineno(function)
              1    0.000    0.000   28.044   28.044 <string>:1(<module>)
           1000   20.304    0.020   27.676    0.028 merge.py:11(f3)
           1000    0.001    0.000   27.677    0.028 merge.py:42(<lambda>)
              1    0.000    0.000    0.000    0.000 timeit.py:143(setup)
              1    0.000    0.000   28.044   28.044 timeit.py:178(timeit)
              1    0.367    0.367   28.044   28.044 timeit.py:96(inner)
              1    0.000    0.000    0.000    0.000 {gc.disable}
              1    0.000    0.000    0.000    0.000 {gc.enable}
              1    0.000    0.000    0.000    0.000 {gc.isenabled}
              1    0.000    0.000    0.000    0.000 {globals}
      100000000    7.372    0.000    7.372    0.000 {method 'append' of 'list' objects}
              1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
              2    0.000    0.000    0.000    0.000 {time.time}
      
      
      None
      f4          2011 function calls in 0.826 seconds
      
         Ordered by: standard name
      
         ncalls  tottime  percall  cumtime  percall filename:lineno(function)
              1    0.000    0.000    0.826    0.826 <string>:1(<module>)
           1000    0.463    0.000    0.463    0.000 merge.py:18(f4)
           1000    0.001    0.000    0.464    0.000 merge.py:42(<lambda>)
              1    0.000    0.000    0.000    0.000 timeit.py:143(setup)
              1    0.000    0.000    0.826    0.826 timeit.py:178(timeit)
              1    0.362    0.362    0.826    0.826 timeit.py:96(inner)
              1    0.000    0.000    0.000    0.000 {gc.disable}
              1    0.000    0.000    0.000    0.000 {gc.enable}
              1    0.000    0.000    0.000    0.000 {gc.isenabled}
              1    0.000    0.000    0.000    0.000 {globals}
              1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
              2    0.000    0.000    0.000    0.000 {time.time}
      
      
      None
      f5          2011 function calls in 4.608 seconds
      
         Ordered by: standard name
      
         ncalls  tottime  percall  cumtime  percall filename:lineno(function)
              1    0.000    0.000    4.608    4.608 <string>:1(<module>)
           1000    4.253    0.004    4.253    0.004 merge.py:24(f5)
           1000    0.001    0.000    4.254    0.004 merge.py:42(<lambda>)
              1    0.000    0.000    0.000    0.000 timeit.py:143(setup)
              1    0.000    0.000    4.608    4.608 timeit.py:178(timeit)
              1    0.354    0.354    4.608    4.608 timeit.py:96(inner)
              1    0.000    0.000    0.000    0.000 {gc.disable}
              1    0.000    0.000    0.000    0.000 {gc.enable}
              1    0.000    0.000    0.000    0.000 {gc.isenabled}
              1    0.000    0.000    0.000    0.000 {globals}
              1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
              2    0.000    0.000    0.000    0.000 {time.time}
      
      
      None
      

      【讨论】:

        猜你喜欢
        • 2016-04-03
        • 1970-01-01
        • 2011-07-19
        • 2014-05-05
        • 1970-01-01
        • 2017-06-10
        • 2010-11-14
        • 1970-01-01
        • 2023-04-09
        相关资源
        最近更新 更多