Print list of lists in separate lines

后端 未结 6 1851
生来不讨喜
生来不讨喜 2020-12-01 10:15

I have a list of lists:

a = [[1, 3, 4], [2, 5, 7]]

I want the output in the following format:

1 3 4
2 5 7
<
6条回答
  •  再見小時候
    2020-12-01 10:40

    oneliner:

    print('\n'.join(' '.join(map(str,sl)) for sl in l))
    

    explanation:
    you can convert list into str by using join function:

    l = ['1','2','3']
    ' '.join(l) # will give you a next string: '1 2 3'
    '.'.join(l) # and it will give you '1.2.3'
    

    so, if you want linebreaks you should use new line symbol.
    But join accepts only list of strings. For converting list of things to list of strings, you can apply str function for each item in list:

    l = [1,2,3]
    ' '.join(map(str, l)) # will return string '1 2 3'
    

    And we apply this construction for each sublist sl in list l

提交回复
热议问题