How do I concatenate two lists in Python?
Example:
listone = [1,2,3]listtwo = [4,5,6]
Expected outcome:
joinedlist == [1, 2, 3, 4, 5, 6]
本文共 8169 字,大约阅读时间需要 27 分钟。
641 77 | How do I concatenate two lists in Python? Example: listone = [1,2,3]listtwo = [4,5,6] Expected outcome: joinedlist == [1, 2, 3, 4, 5, 6] | |||
|
1171 | Python makes this ridiculously easy. mergedlist = listone + listtwo | ||||||||||||||||||||
|
104 | It's also possible to create a generator that simply iterates over the items in both lists. This allows you to chain lists (or any iterable) together for processing without copying the items to a new list: import itertoolsfor item in itertools.chain(listone, listtwo): # do something with each list item | ||||||||||||
|
85 | You can use sets to obtain merged list of unique values mergedlist = list(set(listone + listtwo)) | ||||||||||||||||||||
|
31 | This is quite simple, I think it was even shown in the : >>> listone = [1,2,3]>>> listtwo = [4,5,6]>>>>>> listone + listtwo[1, 2, 3, 4, 5, 6] | ||
|
26 | You could also do listone = [1,2,3]listtwo = [4,5,6]mergedlist = []mergedlist.extend(listone)mergedlist.extend(listtwo) | ||||
|
18 | You could simply use the a = [1, 2, 3]b = [4, 5, 6]c = a + b Or: c = []a = [1, 2, 3]b = [4, 5, 6]c += (a + b) Also, if you want the values in the merged list to be unique you can do: c = list(set(a + b)) | |||
|
11 | It's worth noting that the >>> l1 = ['a']; l2 = ['b', 'c']; l3 = ['d', 'e', 'f']>>> [i for i in itertools.chain(l1, l2)]['a', 'b', 'c']>>> [i for i in itertools.chain(l1, l2, l3)]['a', 'b', 'c', 'd', 'e', 'f'] If an iterable (tuple, list, generator, etc.) is the input, the >>> il = [['a'], ['b', 'c'], ['d', 'e', 'f']]>>> [i for i in itertools.chain.from_iterable(il)]['a', 'b', 'c', 'd', 'e', 'f'] | ||
|
9 | With Python 3.3+ you can use : listone = [1,2,3]listtwo = [4,5,6]def merge(l1, l2): yield from l1 yield from l2>>> list(merge(listone, listtwo))[1, 2, 3, 4, 5, 6] Or, if you want to support an arbitrary number of iterators: def merge(*iters): for it in iters: yield from it>>> list(merge(listone, listtwo, 'abcd', [20, 21, 22]))[1, 2, 3, 4, 5, 6, 'a', 'b', 'c', 'd', 20, 21, 22] | ||
|
7 | If you want to merge the two lists in sorted form, you can use merge function from the heapq library. from heapq import mergea = [1,2,4]b = [2,4,6,7]print list(merge(a,b)) | |||
|
4 | If you need to merge two ordered lists with complicated sorting rules, you might have to roll it yourself like in the following code (using a simple sorting rule for readability :-) ). list1 = [1,2,5]list2 = [2,3,4]newlist = []while list1 and list2: if list1[0] == list2[0]: newlist.append(list1.pop(0)) list2.pop(0) elif list1[0] < list2[0]: newlist.append(list1.pop(0)) else: newlist.append(list2.pop(0))if list1: newlist.extend(list1)if list2: newlist.extend(list2)assert(newlist == [1, 2, 3, 4, 5]) | |||
|
3 | As a more general way for more lists you can put them within a list and use >>> l=[[1, 2, 3], [4, 5, 6], [7, 8, 9]]>>> import itertools>>> list(itertools.chain.from_iterable(l))[1, 2, 3, 4, 5, 6, 7, 8, 9] 1. Note that | ||
|
3 | If you don't want to or can't use the plus operator ( listone = [1,2,3]listtwo = [4,5,6]result = list.__add__(listone, listtwo)print(result)>>> [1, 2, 3, 4, 5, 6] | |||
|
2 | You could use the append() method mergedlist =[]for elem in listone: mergedlist.append(elem)for elem in listtwo: mergedlist.append(elem) | ||||
|
1 | list1 = [1,2,3]list2 = [4,5,6]joinedlist = list1 + list2#output : [1, 2, 3, 4, 5, 6] Yes, its that simple. | ||
|
1 | Joining two lists in Python: >>> a = [1, 2, 3, 4]>>> b = [1, 4, 6, 7]>>> c = a + b >>> c[1, 2, 3, 4, 1, 4, 6, 7] If you don't want any duplication : >>> a = [1, 2, 3, 4, 5, 6]>>> b = [5, 6, 7, 8]>>> c = list(set(a + b)) >>> c[1, 2, 3, 4, 5, 6, 7, 8] | ||
|
1 | This question directly asks about joining two lists. However it's pretty high in search even when you are looking for a way of joining many lists (including the case when you joining zero lists). Consider this more generic approach: a = [[1,2,3], [4,5,6], [7,8,9]]reduce(lambda c, x: c + x, a, []) Will output: [1, 2, 3, 4, 5, 6, 7, 8, 9] Note, this also works correctly when Update Consider better alternative suggested by Patrick Collins in the comments: sum(a, []) | ||||
|
转载地址:http://uihbi.baihongyu.com/
[3,2,1]
? Will the output change? – Nov 12 '09 at 7:06