简体   繁体   English

如何按 Python 中的姓氏对从 txt 文件导入的名称列表进行排序

[英]How to sort a list of names imported from txt file by last name in Python

I just installed Python today, and I want to sort a list of names, that I import from a text file, by their last name.我今天刚刚安装了 Python,我想按姓氏对我从文本文件中导入的名称列表进行排序。 Every name in the txt file is separated by a comma and a space. txt 文件中的每个名称都用逗号和空格分隔。 The reason I use print so often is that I want to follow every step to see where it goes wrong.我经常使用 print 的原因是我想跟踪每一步,看看哪里出了问题。 At first it just returned the same set of names that I put in, and I thought that was due to there being no apostrophes around the names.起初,它只是返回了我输入的相同名称集,我认为这是因为名称周围没有撇号。 So I put in the studenten1 to try to fix that.所以我把studenten1放进去试图解决这个问题。

f = open('namenlijst.txt', 'r')
studenten = f.read()
f.close()
print(studenten)

studenten1 = "'" + studenten.replace(", ", "', '") + "'"
print (studenten1)

print(sorted(studenten1, key=lambda x: x.split(",")[-1]))

Input:输入:

Jeremy Underwood, Louis Malone, Jett Obrien, Lee Cordova, Avery Hill, Amanda Fowler, Callum Ferguson, Hallie Clark, Branson Calhoun

Output: Output:

Jeremy Underwood, Louis Malone, Jett Obrien, Lee Cordova, Avery Hill, Amanda Fowler, Callum Ferguson, Hallie Clark, Branson Calhoun

'Jeremy Underwood', 'Louis Malone', 'Jett Obrien', 'Lee Cordova', 'Avery Hill', 'Amanda Fowler', 'Callum Ferguson', 'Hallie Clark', 'Branson Calhoun'

[',', ',', ',', ',', ',', ',', ',', ',', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", "'", 'A', 'A', 'B', 'C', 'C', 'C', 'C', 'F', 'F', 'H', 'H', 'J', 'J', 'L', 'L', 'M', 'O', 'U', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'b', 'd', 'd', 'd', 'd', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'g', 'h', 'i', 'i', 'i', 'i', 'k', 'l', 'l', 'l', 'l', 'l', 'l', 'l', 'l', 'l', 'l', 'm', 'm', 'm', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 's', 's', 's', 't', 't', 'u', 'u', 'u', 'u', 'v', 'v', 'w', 'w', 'y', 'y']

Expected output:预期 output:

'Branson Calhoun', 'Hallie Clark', 'Lee Cordova', 'Callum Ferguson', 'Amanda Fowler', 'Avery Hill', 'Louis Malone', 'Jett Obrien', 'Jeremy Underwood' “布兰森·卡尔霍恩”、“哈利·克拉克”、“李·科尔多瓦”、“卡勒姆·弗格森”、“阿曼达·福勒”、“艾弗里·希尔”、“路易斯·马龙”、“杰特·奥布莱恩”、“杰里米·安德伍德”

When you use read the output is a string, you can transform your input in list using the split method:当您使用read output 是一个字符串时,您可以使用split方法转换列表中的输入:

f = open('namenlijst.txt', 'r')
studenten = f.read()
f.close()
studenten1 = studenten.split(",")

print(sorted(studenten1, key=lambda x: x.split()[-1]))

And you have you desired output:你有你想要的output:

[' Branson Calhoun', ' Hallie Clark', ' Lee Cordova', ' Callum Ferguson', ' Amanda Fowler', ' Avery Hill', ' Louis Malone', ' Jett Obrien', 'Jeremy Underwood']

f.read() is a string, so your output is sorting a single string containing all the names. f.read()是一个字符串,因此您的 output 正在对包含所有名称的单个字符串进行排序。 You will want to do f.read().split(',') instead to put output into list (assuming all entries are on single line as you've written).您将需要执行f.read().split(',')而不是将 output 放入列表中(假设所有条目都在您编写的单行上)。

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

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