[英]How to concatenate a list of tuples into a list of concatenated item from the tuples in python?
我有一個元組列表,我想列出由元組中的每個項目串聯而成的簡單字符串。 即:
a = [("as","b","c"),("d","e"),("f","g")]
b將是= ["as bc","de","fg"]
我嘗試了這個:
b = [sum(i,[]) for i in a ]
但是我得到了
TypeError: can only concatenate tuple (not "str" to tuple)`
要使用空格連接多個字符串,應使用str.join()
。 由於您在要用於連接其他字符串的字符串上調用它,因此需要一點時間來適應它,在本例中為空格字符:
>>> map(" ".join, a)
['as b c', 'd e', 'f g']
有些人對此更喜歡列表理解:
[" ".join(t) for t in a]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.