有没有办法用\n
序列分隔列表项?
这是代码:
month_names = [
['January', 'February', 'March'],
['April', 'May', 'June'],
['July', 'August', 'September'],
['October', 'November', 'December'],
]
print month_names
问题是当我尝试将\n
或'\n'
添加到列表或打印时:
print month_names + '\n'
或
['January', 'February', 'March'], '\n',
语法或类型错误引发,有时打印\ n。
所需的输出是:
'January', 'February', 'March'
'April', 'May', 'June'
'July', 'August', 'September'
'October', 'November', 'December'
我希望将其保留为列表,以便我可以回忆起第二个列表:
'April', 'May', 'June'
答案 0 :(得分:3)
我相信你想要的是个别月份与新行的简单连接
>>> print '\n'.join(map('\n'.join, month_names))
January
February
March
April
May
June
July
August
September
October
November
December
如果你想要每行中的宿舍,下列其中一个应该可以工作
>>> print '\n'.join(map(' '.join, month_names))
January February March
April May June
July August September
October November December
>>> print '\n'.join(map(str, month_names))
['January', 'February', 'March']
['April', 'May', 'June']
['July', 'August', 'September']
['October', 'November', 'December']