[英]Python: How can I take a list of lists, convert every element into strings, and return the list of lists?
例如:
list = [[11,2,3,5],[5,3,74,1,90]]
返回同一件事,只有所有內容都是str而不是int。 我希望能夠在它們上使用.join。 謝謝!
如果您只深入2個列表:
>>> l = [[11, 2, 3, 5], [5, 3, 74, 1, 90]]
>>> [[str(j) for j in i] for i in l]
[['11', '2', '3', '5'], ['5', '3', '74', '1', '90']]
我將為此使用list-comp並map
:
[ map(str,x) for x in lst ]
但是我想py3.x在那里需要一個附加list
(糟糕)。
[ list(map(str,x)) for x in lst ]
附帶說明一下,無論如何我們都無法在此列表上使用join
。 我猜你想做這樣的事情:
for x in lst:
print ("".join(x))
如果是這種情況,您可以一起放棄轉換,而在加入時只需這樣做:
for x in lst:
print ("".join(str(item) for item in x))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.