我有一个包含所有整数的2D列表。我想将所有整数转换为字符串:
L = [[1,2,3,4,5],[6,7,8,9,10]]
L = [['1','2','3','4','5'],['6','7','8','9','10']] # convert into string.
现在按照以下格式打印出来:
1 - > 2 - > 3 - > 4 - > 5
6 - > 7 - > 8 - > 9 - > 10
我尝试使用这些代码:
final_out = [i for i in final_out str(ii) for ii in i]
它不起作用。
答案 0 :(得分:2)
您可以使用map
功能:
>>> L = [[1,2,3,4,5],[6,7,8,9,10]]
>>> L = [map(str, l) for l in L] # Convert into list of 'str'
>>> L
[['1', '2', '3', '4', '5'], ['6', '7', '8', '9', '10']]
>>> for l in L:
... print ' -> '.join(l)
...
1 -> 2 -> 3 -> 4 -> 5
6 -> 7 -> 8 -> 9 -> 10
编写上述代码的简单方法是:
>>> L = [[1,2,3,4,5],[6,7,8,9,10]]
>>> for l in L:
... print ' -> '.join(map(str, l))
答案 1 :(得分:0)
这样做:
1 -> 2 -> 3 -> 4 -> 5
6 -> 7 -> 8 -> 9 -> 10
输出:
str(num)
由于我使用了L
,您可以保持create table chunks(
doc varchar,
sentenceid int,
chunkid int,
chunk bit(10)
);
相同。也就是说,您不需要将它们转换为整数。
答案 2 :(得分:0)
您也可以使用nested loop
:
>>> L = [[1,2,3,4,5],[6,7,8,9,10]]
>>> new_L = [[str(j) for j in i] for i in L]
>>> new_L
[['1', '2', '3', '4', '5'], ['6', '7', '8', '9', '10']]
>>> for i in new_L:
print ' -> '.join(i)
1 -> 2 -> 3 -> 4 -> 5
6 -> 7 -> 8 -> 9 -> 10
>>>