I have a 2D list of characters in this fashion:
a = [['1','2','3'],
['4','5','6'],
['7','8','9']]
What's the most pythonic way to print the list as a whole block? Ie no commas or brackets:
123
456
789
There are a lot of ways. Probably a str.join
of a mapping of str.join
s:
>>> a = [['1','2','3'],
... ['4','5','6'],
... ['7','8','9']]
>>> print('\n'.join(map(''.join, a)))
123
456
789
>>>
Best way in my opinion would be to use print
function. With print
function you won't require any type of joining and conversion(if all the objects are not strings).
>>> a = [['1','2','3'],
... ['4', 5, 6], # Contains integers as well.
... ['7','8','9']]
...
>>> for x in a:
... print(*x, sep='')
...
...
123
456
789
If you're on Python 2 then print function can be imported using from __future__ import print_function
.
Like this:
import os
array = [['1','2','3'],
['4','5','6'],
['7','8','9']]
print(os.linesep.join(map(''.join, array)))
If you're looking for Pythonic then you surely need a generator comprehension:
print('\n'.join(''.join(i) for i in array))
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.