2014-04-23 31 views
0

如何在Python中加入兩個字符列表?在Python中加入兩個字符列表

例子:

listone = [a,b,c] 
listtwo = [d,e,f] 

輸出:

Joinedlist == [ad, be, cf] 
+1

你得到一些質量答案對於你的問題,但爲了將來的參考,請記住包括一些信息關於你*已經試圖解決你的問題中的某個問題。相關/接壤重複:[在python中加入兩個列表到元組中](http://stackoverflow.com/q/21196165/1199226) – itsjeyd

回答

3

首先,您的字符應該是單/雙引號:

listone = ['a', 'b', 'c'] 
listtwo = ['d', 'e', 'f'] 

然後,你可以這樣做:

listthree = [i+j for i,j in zip(listone,listtwo)] 

>>> print listthree 
['ad', 'be', 'cf'] 
4

使用map

>>> from operator import add 
>>> one = ["a", "b", "c"] 
>>> two = ["d", "e", "f"] 
>>> map(add, one, two) 
['ad', 'be', 'cf'] 
+0

你可以使用'str .__ add__'來代替導入add運算符 – Elisha

+0

@Elisha確實,但是對於更大的列表,它可能會慢一點。 –

+0

不是'add'運算符只是使用'str .__ add__'? – Elisha

2

您可以使用列表中理解和zip()方法 -

print [m + n for m, n in zip(listone, listtwo)] 
0

你也可以使用join代替+

print [''.join(x) for x in zip(listone, listtwo)]