[英]Python: How can I take a list of lists, convert every element into strings, and return the list of lists?
For example: 例如:
list = [[11,2,3,5],[5,3,74,1,90]]
returns the same thing, only everything is a str instead of an int. 返回同一件事,只有所有内容都是str而不是int。 I want to be able to use .join on them. 我希望能够在它们上使用.join。 Thanks! 谢谢!
If you only ever go 2 lists deep: 如果您只深入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']]
I'd use a list-comp and map
for this one: 我将为此使用list-comp并map
:
[ map(str,x) for x in lst ]
But I suppose py3.x would need an addition list
in there (yuck). 但是我想py3.x在那里需要一个附加list
(糟糕)。
[ list(map(str,x)) for x in lst ]
As a side note, you can't use join
on this list we return anyway. 附带说明一下,无论如何我们都无法在此列表上使用join
。 I'm guessing you want to do something like this: 我猜你想做这样的事情:
for x in lst:
print ("".join(x))
If that's the case, you can forgo the conversion all together and just do it when you're joining: 如果是这种情况,您可以一起放弃转换,而在加入时只需这样做:
for x in lst:
print ("".join(str(item) for item in x))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.