简体   繁体   中英

Printing nested lists of a list on different lines

I have the following list:

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

I can get the following result:

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

by using this code:

for n in l:
    print n

How can I get the following results which are not list:

1, 2, 3
4, 5
6

I am trying this:

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

but it gives this:

1
2
3
4
5
6

With:

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

You will get:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM