如何在Python中从文本文件对数据进行最高到最低排序? [英] How do I sort data highest to lowest in Python from a text file?
问题描述
我尝试了多种方法来执行此操作,但似乎都没有用 答案按字母顺序出现
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:
- 在
getlinevalue
中,我们在空格上分割提供的字符串(str.split
在未提供参数的情况下执行此操作),然后使用[-1]
从拆分中获取最后一个值(从负数开始索引从末尾开始).因此,类似'Dave, 10'
的内容将作为'10'
存储到intpart
.然后,使用int()
'10'转换为其整数值>并将其退回 -
with open("classt2.txt") as f:
打开文件进行读取,并将结果分配给f
;当缩进的with
块完成时,该文件将为您关闭(即使该块由于异常或从函数返回而退出) -
stripnewlines = (line.rstrip() for line in f)
创建一个生成器表达式(就像懒惰的列表理解一样,只能重复一次)一次读取一行并使用str.rstrip()
删除所有结尾的空格(例如,换行;您可以使用str.rstrip("\r\n")
仅删除换行,而不删除结尾的制表符或空格,但需要调整key
函数).我们可以使用列表理解而不是生成器表达式,但是由于sorted
仍然会为我们创建list
,因此我们很懒于避免同时存储已排序和未排序的列表(或者,列表理解之后,可以对结果进行.sort(...)
调用,这也可以避免在内存中保留两个列表,但是.sort(...)
不返回任何内容,因此我们将有更多的代码行. -
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
来反转整数的自然"排序 - 最后的单行代码使用"splat"运算符(
*
)将sorted
调用产生的列表转换为print
函数(或者您将print
函数在Python 2中使用from __future__ import print_function
替换Py2的常规print
语句),将打印每个参数,并在每个参数之间打印sep
(默认为单个空格' '
),并且所有参数都打印后,紧随其后的是end
的值(默认为新行"\n"
).这样一来,您就可以在单独的输出行上从最高到最低打印输入行,而不是在单行上打印排序列表的表示形式.
- 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 tointpart
as'10'
. Then we convert the string'10'
to its integer value withint()
and return it with open("classt2.txt") as f:
opens the file for read and assigns the result tof
; when the indentedwith
block finishes, the file is closed for you (even if the block exits due to exceptions or returning from a function)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 usesstr.rstrip()
to remove all trailing whitespace (e.g. the new line; you could usestr.rstrip("\r\n")
to only remove the newline, not trailing tabs or spaces, but thekey
function would need to be tweaked). We could use a list comprehension instead of a generator expression, but sincesorted
will create thelist
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).sorted(stripnewlines, reverse=True, key=getlinevalue)
is just like thesorted
you used in your attempt except it sorts each (rstrip
-ed) line based on the result of callinggetlinevalue
on it (it only calls thekey
function once per value, which is whykey
is superior tocmp
;cmp
would have to convert each valuelog(n)
times during the sort, on average, orn log(n)
conversions total;key
converts each value once, and performs a total ofn
conversions). So it sorts'Dave, 10'
relative to'Anne, 3'
by comparing the result of callinggetlinevalue('Dave, 10')
(10
) togetlinevalue('Anne, 3')
(3
). Since numbers sort in ascending order (lowest to highest) normally (so3
would sort before10
) and you want descending order (highest to lowest) we also passreverse=True
to reverse the "natural" sort of the integers.- The final one-liner uses the "splat" operator (
*
) to convert the list resulting from thesorted
call to sequential positional arguments toprint
; for Python 3'sprint
function (or theprint
function you get in Python 2 withfrom __future__ import print_function
that replaces the normalprint
statement of Py2), each argument is printed, withsep
printed between each argument (defaults to a single space,' '
), and when all arguments are printed, follows it up with the value ofend
(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屋!