How do I sort a text file by three columns with a specific order to those columns in Python?

亡梦爱人 提交于 2021-02-08 12:18:53

问题


How do I sort a text file by three columns with a specific order to those columns in Python?

My text_file is in the following format with whitespaces between columns:

Team_Name Team_Mascot Team_Color Team_Hometown Number_of_Wins Team_Coach

Example:

Bears LittleBear Blue Beartown 15 BigBear

I want to sort it by Team_Color first, Team_Hometown second, and Number_of_Wins third in ascending order.

Therefore the attributes:

Bears Blue Beartown 15 Coach1
Bears Red  Dogtown 30 Coach6
Bears Blue Cattown 15 Coach2
Bears Red  Beartown 15 Coach4
Bears Blue Cattown 17 Coach3
Bears Red  Dogtown 9 Coach5

My expected output is a sorted text file:

Bears Blue Beartown 15 Coach1
Bears Blue Cattown 15 Coach2
Bears Blue Cattown 17 Coach3
Bears Red  Beartown 15 Coach4
Bears Red  Dogtown 9 Coach5
Bears Red  Dogtown 30 Coach6

I have thought about using lambda but none of my values are a tuple https://docs.python.org/3/tutorial/controlflow.html

I have looked at previous StackOverflow questions but most of them dealt with sorting two columns at maximum using lambda, tuple, or other methods


回答1:


Your question is still ambiguous. Your example doesn't have the first field Team_Name introduced in your header. So the index here is probably off by one, but I think you get the concept:

#read lines of text file and split into words
lines = [line.split() for line in open("test.txt", "r")]
#sort lines for different columns, numbers converted into integers to prevent lexicographical sorting
lines.sort(key = lambda x: (x[1], x[2], int(x[3])))
#writing the sorted list into another file
with open("new_test.txt", "w") as f:
    for item in lines:
        f.write(" ".join(item) + "\n")



回答2:


You can do it using itemgetter from the operator module:

from operator import itemgetter

def showList(inList):
    for i in inList:
        print(i)

lines = []

with open("test.txt", "r") as infile:
    lines = [i.split() for i in infile.readlines()]
    lines = [[int(j) if j.isdigit() else j for j in i] for i in lines]
    showList(lines)
    lines = sorted(lines, key=itemgetter(1,2,3))
    print()
    showList(lines)

with open("output.txt", "w") as outfile:
    for line in lines:
        outfile.write(" ".join(str(i) for i in line) + "\n")

Output (using showList):

['Bears', 'Blue', 'Beartown', 15, 'Coach1']
['Bears', 'Red', 'Dogtown', 30, 'Coach6']
['Bears', 'Blue', 'Cattown', 15, 'Coach2']
['Bears', 'Red', 'Beartown', 15, 'Coach4']
['Bears', 'Blue', 'Cattown', 17, 'Coach3']
['Bears', 'Red', 'Dogtown', 9, 'Coach5']

['Bears', 'Blue', 'Beartown', 15, 'Coach1']
['Bears', 'Blue', 'Cattown', 15, 'Coach2']
['Bears', 'Blue', 'Cattown', 17, 'Coach3']
['Bears', 'Red', 'Beartown', 15, 'Coach4']
['Bears', 'Red', 'Dogtown', 9, 'Coach5']
['Bears', 'Red', 'Dogtown', 30, 'Coach6']

Output format in new file:

Bears Blue Beartown 15 Coach1
Bears Blue Cattown 15 Coach2
Bears Blue Cattown 17 Coach3
Bears Red Beartown 15 Coach4
Bears Red Dogtown 9 Coach5
Bears Red Dogtown 30 Coach6


来源:https://stackoverflow.com/questions/48726710/how-do-i-sort-a-text-file-by-three-columns-with-a-specific-order-to-those-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!