Python 如何打印列表的列表

2023-12-02

我想用下面的代码打印 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作为函数,不需要联接或显式字符串转换。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python 如何打印列表的列表 的相关文章

随机推荐