我想用下面的代码打印python 3.x中的列表列表,但是它给出了一个错误.
lol=[[1,2],[3,4],[5,6],['five','six']]
for elem in lol:
print (":".join(elem))
# this is the error I am getting-> TypeError: sequence item 0: expected str instance, int found
Run Code Online (Sandbox Code Playgroud)
我期待这个输出:
1:2
3:4
5:6
five:six
Run Code Online (Sandbox Code Playgroud)
我可以使用下面的perl代码实现相同的输出(这仅供参考):
for (my $i=0;$i<scalar(@{$lol});$i++)
{
print join(":",@{$lol->[$i]})."\n";
}
Run Code Online (Sandbox Code Playgroud)
我怎么在python 3.x中做到这一点?
我会去:
for items in your_list:
print (*items, sep=':')
Run Code Online (Sandbox Code Playgroud)
这利用了print作为一个函数,不需要连接或显式字符串转换.
| 归档时间: |
|
| 查看次数: |
471 次 |
| 最近记录: |