我想用下面的代码在 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
我期待这样的输出:
1:2
3:4
5:6
five:six
我可以使用下面的 perl 代码实现相同的输出(仅供引用):
for (my $i=0;$i<scalar(@{$lol});$i++)
{
print join(":",@{$lol->[$i]})."\n";
}
如何在 python 3.x 中执行此操作?
请您参考如下方法:
我会去:
for items in your_list:
print (*items, sep=':')
这将 print 用作函数,不需要连接或显式字符串转换。
