如何在Python中从文本文件对数据进行最高到最低排序? [英] How do I sort data highest to lowest in Python from a text file?

查看:994
本文介绍了如何在Python中从文本文件对数据进行最高到最低排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了多种方法来执行此操作,但似乎都没有用 答案按字母顺序出现

I have tried multiple methods in doing this but none of them seem to work The answer comes up alphabetically instead

f=open("class2.txt", "r")
scores=myfile.readlines()
print(sorted(scores))
f.close()

['Anne,   3\n', 'Dave,   10', 'Jack,   4\n', 'Lucy,   8\n']

在进入shell时,还有什么方法可以摆脱"/n"吗?

Also is there any way to get rid of the "/n" when it goes to the shell?

推荐答案

基于输入和输出,我猜测您正在尝试按关联值对输入名称进行排序.要进行数字排序,您可以解析所有值对,也可以将key函数与sorted配合使用(无需将结果存储在任何地方).例如:

Based on the inputs and outputs, I'm guessing you're trying to sort the input names by the associated values. To sort numerically, you can either parse all the values pairs, or use a key function with sorted that does it for you (without storing the result anywhere). For example:

# This could be a lambda function, but I'm splitting it out for clarity
def getlinevalue(line):
    intpart = line.split()[-1]  # Get the last whitespace separated group (assumed to be legal integer)
    return int(intpart)  # Convert to int, which will sort numerically

with open("classt2.txt") as f:
    stripnewlines = (line.rstrip() for line in f)
    # reverse=True needed to sort highest to lowest; natural sort for int is lowest to highest
    print(sorted(stripnewlines, reverse=True, key=getlinevalue))
    # If the goal is to print one pair per line, replace the print above with:
    for x in sorted(stripnewlines, reverse=True, key=getlinevalue):
        print(x)
    # Or as a one liner (require Py3, or from __future__ import print_function on Py2):
    print(*sorted(stripnewlines, reverse=True, key=getlinevalue), sep="\n")

print(sorted(stripnewlines, reverse=True, key=getlinevalue))的输出将是(对一些空格进行模数化;屏幕截图使很难分辨逗号后有多少空格,所以我只使用一个空格):

The output from print(sorted(stripnewlines, reverse=True, key=getlinevalue)) would be (modulo some whitespace; the screenshot makes it hard to tell how much whitespace is after the comma, so I'm just using a single space):

['Dave, 10', 'Lucy, 8', 'Jack, 4', 'Anne, 3']

这就是您想要的.

注释中要求的代码说明:

Explanation of code as requested in the comments:

  1. getlinevalue中,我们在空格上分割提供的字符串( str.split 在未提供参数的情况下执行此操作),然后使用[-1]从拆分中获取最后一个值(从负数开始索引从末尾开始).因此,类似'Dave, 10'的内容将作为'10'存储到intpart.然后,使用 int() '10'转换为其整数值>并将其退回
  2. with open("classt2.txt") as f:打开文件进行读取,并将结果分配给f;当缩进的 with完成时,该文件将为您关闭(即使该块由于异常或从函数返回而退出)
  3. stripnewlines = (line.rstrip() for line in f)创建一个生成器表达式(就像懒惰的列表理解一样,只能重复一次)一次读取一行并使用 str.rstrip() 删除所有结尾的空格(例如,换行;您可以使用str.rstrip("\r\n")仅删除换行,而不删除结尾的制表符或空格,但需要调整key函数).我们可以使用列表理解而不是生成器表达式,但是由于sorted仍然会为我们创建list,因此我们很懒于避免同时存储已排序和未排序的列表(或者,列表理解之后,可以对结果进行.sort(...)调用,这也可以避免在内存中保留两个列表,但是.sort(...)不返回任何内容,因此我们将有更多的代码行.
  4. sorted(stripnewlines, reverse=True, key=getlinevalue)就像您使用的 sorted 在尝试中,除了根据调用getlinevalue的结果对每行(rstrip -ed)进行排序(每个值仅调用key函数一次,这就是key优于; cmp必须在排序过程中将每个值平均转换log(n)次,或总计n log(n)次转换; key将每个值转换一次,并执行总计n次转换).因此,通过比较调用getlinevalue('Dave, 10')(10)与getlinevalue('Anne, 3')(3)的结果,相对于'Anne, 3''Dave, 10'进行了排序.由于数字通常按升序排序(从最低到最高)(所以3会在10之前排序),而您想要降序(从最高到最低),我们也通过了reverse=True来反转整数的自然"排序
  5. 最后的单行代码使用"splat"运算符(*)将sorted调用产生的列表转换为 print函数(或者您将print函数在Python 2中使用from __future__ import print_function替换Py2的常规print语句),将打印每个参数,并在每个参数之间打印sep(默认为单个空格' '),并且所有参数都打印后,紧随其后的是end的值(默认为新行"\n").这样一来,您就可以在单独的输出行上从最高到最低打印输入行,而不是在单行上打印排序列表的表示形式.
  1. In getlinevalue, we're splitting the provided string on whitespace (str.split does this when not given an argument), then taking the last value from the split with [-1] (indexing with negative numbers starts from the end). So something like 'Dave, 10' is stored to intpart as '10'. Then we convert the string '10' to its integer value with int() and return it
  2. with open("classt2.txt") as f: opens the file for read and assigns the result to f; when the indented with block finishes, the file is closed for you (even if the block exits due to exceptions or returning from a function)
  3. stripnewlines = (line.rstrip() for line in f) Creates a generator expression (sort of like a lazily evaluated list comprehension that can only be iterated once) that reads a line at a time and uses str.rstrip() to remove all trailing whitespace (e.g. the new line; you could use str.rstrip("\r\n") to only remove the newline, not trailing tabs or spaces, but the key function would need to be tweaked). We could use a list comprehension instead of a generator expression, but since sorted will create the list for us anyway, we're being lazy to avoid having both the sorted and unsorted list stored at the same time (alternatively, a list comprehension could be followed by a .sort(...) call on the result, which would also avoid keeping two lists in memory, but .sort(...) doesn't return anything, so we'd have more lines of code).
  4. sorted(stripnewlines, reverse=True, key=getlinevalue) is just like the sorted you used in your attempt except it sorts each (rstrip-ed) line based on the result of calling getlinevalue on it (it only calls the key function once per value, which is why key is superior to cmp; cmp would have to convert each value log(n) times during the sort, on average, or n log(n) conversions total; key converts each value once, and performs a total of n conversions). So it sorts 'Dave, 10' relative to 'Anne, 3' by comparing the result of calling getlinevalue('Dave, 10') (10) to getlinevalue('Anne, 3') (3). Since numbers sort in ascending order (lowest to highest) normally (so 3 would sort before 10) and you want descending order (highest to lowest) we also pass reverse=True to reverse the "natural" sort of the integers.
  5. The final one-liner uses the "splat" operator (*) to convert the list resulting from the sorted call to sequential positional arguments to print; for Python 3's print function (or the print function you get in Python 2 with from __future__ import print_function that replaces the normal print statement of Py2), each argument is printed, with sep printed between each argument (defaults to a single space, ' '), and when all arguments are printed, follows it up with the value of end (defaults to a new line, "\n"). So that would let you print the input lines from highest to lowest on separate output lines rather than printing the representation of the sorted list on a single line.

这篇关于如何在Python中从文本文件对数据进行最高到最低排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆