簡體   English   中英

在不同的行上打印列表的嵌套列表

[英]Printing nested lists of a list on different lines

我有以下清單:

l = [[1,2,3],[4,5],[6]]

我可以得到以下結果:

[1, 2, 3]
[4, 5]
[6]

通過使用以下代碼:

for n in l:
    print n

如何獲得以下未列出的結果:

1, 2, 3
4, 5
6

我正在嘗試:

for n in l:
    for s in n:
        print s

但這給出了:

1
2
3
4
5
6

附:

for n in l:
    for s in n:
        print s,
    print

你會得到:

1 2 3
4 5
6
>>> l = [[1,2,3],[4,5],[6]]
>>> for ns in l:
...     print ', '.join(map(str, ns))
...
1, 2, 3
4, 5
6
>>> for ns in l:
...     print str(ns)[1:-1]
...
1, 2, 3
4, 5
6
>>> print '\n'.join(str(n).strip('[]') for n in l)
1, 2, 3
4, 5
6

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM