简体   繁体   English

逐行打印python列表

[英]Print a python list line by line

How can I get the results of my print onto new lines? 如何获得打印结果到新行? The list being printed is a first name, surname and email address, so I want each one on a new line. 正在打印的列表是名字,姓氏和电子邮件地址,所以我希望每个人都换一行。

import csv

f = open('attendees1.csv')
csv_f = csv.reader(f)

attendee_details = []

for row in csv_f:
    attendee_details.append(row[0:4])



sortedlist =[sorted(attendee_details, key=lambda x:x[0])]

print (sortedlist)
print('\n'.join(map(str, sortedlist)))

This will work for all lists. 这将适用于所有列表。 If the list already consists of strings, the map is not needed. 如果列表已经由字符串组成,则不需要map

Since you're using Python 3, you can also do this: 由于您使用的是Python 3,因此您也可以执行以下操作:

print(*sortedlist, sep='\n')

This unpacks the list and sends each element as an argument to print() , with '\\n' as a separator. 这将解压缩列表,并将每个元素作为参数发送给print() ,并以'\\n'作为分隔符。

Use a for loop. 使用for循环。 ie, replace the last two lines with this 即,用此替换最后两行

for i in sorted(attendee_details, key=lambda x:x[0]):
    print(i)

Note that 注意

sortedlist =[sorted(attendee_details, key=lambda x:x[0])]

line of code will create list of list. 这行代码将创建列表列表。

最简单的方法如下:

print(*sorted(runnerup), sep='\n')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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