回答
試試這個,
>>> a
[[1, 2, 3], [4, 5, 6]]
>>> result=[]
>>> for i in a:
result+=i
>>> result
[1, 2, 3, 4, 5, 6]
>>>
OR
>>> a
[[1, 2, 3], [4, 5, 6]]
>>> sum(a, [])
輸出:
[1, 2, 3, 4, 5, 6]
OR
>>> a1
[1, 2, 3]
>>> a2
[4, 5, 6]
>>> [item for item in itertools.chain(a1, a2)]
輸出:
[1, 2, 3, 4, 5, 6]
如果:
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
@ user3531797,'a'是你替換的 – sshashank124
@ user3531797,查看更新後的答案。 – sshashank124
我會做,使用itertools.chain
:
>>> import itertools
>>> a = [[1, 2, 3], [4 , 5, 6]]
>>> a = itertools.chain.from_iterable(a)
>>> a
[1, 2, 3, 4, 5, 6]
listtwo = [8,9,0]
listone = [1,2,3,4,5]
mergedlist = listone + listtwo
這裏我收集了一些解決方案,只是嘗試timeit:
這裏是我的代碼片段:
#!/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
- 1. 如何合併列表中的項目<>集合C#
- 2. 合併列表中的項目
- 3. 合併列表中的重疊項目
- 4. 在SharePoint 2007中合併項目列表
- 5. 如何將列表中的兩個項目合併爲一個?
- 6. 如何將多個列表項合併爲一個項目?
- 7. 如何將SharePoint列表項目合併到Word 2003文檔中
- 8. 合併列表+更新項目
- 9. WPF合併項目源列表
- 10. Python從兩個列表合併項目
- 11. 如何合併flatMap中的項目MapIterable
- 12. 如何將python列表和共享項目合併到新列表中
- 13. 列表的組合項目
- 14. 如何通過組合框列出項目列表框中的項目
- 15. 合併包含在列表框中的項目的邊框
- 16. 合併在列表中的項目,從不同的價值
- 17. 合併列表中的重複項
- 18. 如何更新(合併)當表中存在類似的項目
- 19. 給定列表中的項目組合
- 20. Android ListView中的複合列表項目
- 21. 結合列表中的類似項目
- 22. Android中的複合列表項目
- 23. 將單獨列表中的項目合併到嵌套列表中
- 24. 將兩個列表框中的項目合併到第三個列表框中
- 25. 合併項目
- 26. 結合項目列表
- 27. 根據列表中的項目合併列表的某些部分 - Python
- 28. 如何合併列表中的幀?
- 29. 如何合併PROLOG中的列表?
- 30. 如何將每個列表逐項合併成n個列表
你可以請爲清晰起見提供更多的代碼 – sshashank124
變量(或Python中的「名稱」)一次只能是一個對象。你有什麼是包含兩個列表的列表(或類似),分別包含1,2,3和4,5,6。 – Benproductions1
可能重複[在Python中合併兩個列表?](http://stackoverflow.com/questions/1720421/merge-two-lists-in-python) – BlaShadow