简体   繁体   中英

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. Every name in the txt file is separated by a comma and a space. The reason I use print so often is that I want to follow every step to see where it goes wrong. 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.

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:

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:

'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:

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:

[' 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. 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).

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.

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