python中
a = [['z', 'G', 'j', 'E', 'U'], ['%', '#', '(', '!', ')'], ['6', '4', '8', '1', '3']]
我想把上面的嵌套列表打印成并排的字符示例输出:
zGjEU%#(!)64813
怎么做?
考虑一个嵌套式推导和str.join
:
>>> a = [['z', 'G', 'j', 'E', 'U'], ['%', '#', '(', '!', ')'], ['6', '4', '8', '1', '3']]
>>> print(''.join(c for chars in a for c in chars))
zGjEU%#(!)64813
a = [['z', 'G', 'j', 'E', 'U'], ['%', '#', '(', '!', ')'], ['6', '4', '8', '1', '3']]
result = ""
for item in a:
for char in item:
result += char
结果:
'zGjEU%#(!)64813'