简体   繁体   English

如何按三列对文本文件进行排序,并按照 Python 中这些列的特定顺序进行排序?

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

How do I sort a text file by three columns with a specific order to those columns in Python?如何按三列对文本文件进行排序,并按照 Python 中这些列的特定顺序进行排序?

My text_file is in the following format with whitespaces between columns:我的 text_file 采用以下格式,列之间有空格:

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.我想先按 Team_Color 排序,第二个是 Team_Hometown,第三个是 Number_of_Wins。

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我曾考虑使用 lambda,但我的值都不是元组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我看过以前的 StackOverflow 问题,但其中大多数是使用 lambda、tuple 或其他方法处理最多对两列进行排序

Your question is still ambiguous.你的问题还是模棱两可。 Your example doesn't have the first field Team_Name introduced in your header.您的示例没有在标题中引入的第一个字段 Team_Name。 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")

You can do it using itemgetter from the operator module:您可以使用operator模块中的itemgetter来完成:

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 ):输出(使用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

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

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