如何在python中对外部列表排序
我目前对如何订购外部文件感到困惑。我目前有一个名为“nomes”的文件(它是一个.txt文件)。我希望能够从文件中获取所有值并按大小排序。值越大,列表中的值越高。我试图通过气泡排序来实现这一点 我目前的问题是,我不认为我实际上是把这些名词输入到我在python代码中设置的列表中 下面是代码(可能比我的英语更有意义;): 以下是文件“nomes.txt”:如何在python中对外部列表排序,python,file,list,external,Python,File,List,External,我目前对如何订购外部文件感到困惑。我目前有一个名为“nomes”的文件(它是一个.txt文件)。我希望能够从文件中获取所有值并按大小排序。值越大,列表中的值越高。我试图通过气泡排序来实现这一点 我目前的问题是,我不认为我实际上是把这些名词输入到我在python代码中设置的列表中 下面是代码(可能比我的英语更有意义;): 以下是文件“nomes.txt”: 您不需要编写自己的排序算法,Python已经涵盖了这一点。您还可以更轻松地读取数据: with open("nouns.txt") as fi
您不需要编写自己的排序算法,Python已经涵盖了这一点。您还可以更轻松地读取数据:
with open("nouns.txt") as file:
nouns = []
for line in file:
noun, count = line.split(",")
nouns.append((int(count), noun))
现在,您可以轻松地按计数进行排序(因为它现在是元组中的第一项):
然后再次打印结果:
for count, noun in nouns:
print("{0},{1}".format(noun,count))
您不需要编写自己的排序算法,Python已经涵盖了这一点。您还可以更轻松地读取数据:
with open("nouns.txt") as file:
nouns = []
for line in file:
noun, count = line.split(",")
nouns.append((int(count), noun))
现在,您可以轻松地按计数进行排序(因为它现在是元组中的第一项):
然后再次打印结果:
for count, noun in nouns:
print("{0},{1}".format(noun,count))
如果您关心性能,NumPy是一个很好的工具:
import numpy as np
dtype=[('name', object), ('score', int)]
data = np.loadtxt('nouns.txt', delimiter=',', dtype=dtype)
data.sort(order='score')
print data
如果您关心性能,NumPy是一个很好的工具:
import numpy as np
dtype=[('name', object), ('score', int)]
data = np.loadtxt('nouns.txt', delimiter=',', dtype=dtype)
data.sort(order='score')
print data
如果我理解正确,这将满足您的要求:
with open("nouns.txt", "r")as file:
nouns = file.read().splitlines()
# for each line separete the noun from the value
nouns = [noun.strip().split(',') for noun in nouns]
# now cast it to an int so you can sort by it
nouns = [(x[0], int(x[1])) for x in nouns]
# sorted will do the rest
sorted_nouns = sorted(nouns, key=lambda n: n[1])
sorted_nouns_reverse = sorted(nouns, key=lambda n: n[1], reverse=True)
# you can also use list method .sort() to sort it in place
nouns.sort(key=lambda n: n[1])
print(nouns)
# and of course reversed
nouns.sort(key=lambda n: n[1], reverse=True)
print(nouns)
print(sorted_nouns, sorted_nouns_reverse)
现在只需再次将其保存到文件中即可如果我理解正确,这将满足您的要求:
with open("nouns.txt", "r")as file:
nouns = file.read().splitlines()
# for each line separete the noun from the value
nouns = [noun.strip().split(',') for noun in nouns]
# now cast it to an int so you can sort by it
nouns = [(x[0], int(x[1])) for x in nouns]
# sorted will do the rest
sorted_nouns = sorted(nouns, key=lambda n: n[1])
sorted_nouns_reverse = sorted(nouns, key=lambda n: n[1], reverse=True)
# you can also use list method .sort() to sort it in place
nouns.sort(key=lambda n: n[1])
print(nouns)
# and of course reversed
nouns.sort(key=lambda n: n[1], reverse=True)
print(nouns)
print(sorted_nouns, sorted_nouns_reverse)
现在只需再次将其保存到文件中如何将输入文本作为文本,以便我们可以复制它?很抱歉,我不太明白这一点?不要在图像文件中发布文本。发布文本。好的,我下次会记住。把输入文本作为文本,这样我们就可以复制它了。很抱歉,我不太明白。不要在图像文件中发布文本。发短信。好的,下次我会记得的。有些东西对我来说真的很新鲜。我的意思是,我在python方面还没有那么先进。但是谢谢你的帮助。我会及时弄明白的。再次感谢您。NumPy的文档非常好,因此我建议您只需阅读loadtxt(函数)和dtype(概念)的文档。从这里开始,只需调用数组的sort()方法,按任何命名列进行就地排序。使用NumPy的主要优点是它用C实现了数组操作,可以非常非常快。有些东西对我来说真的很新。我的意思是,我在python方面还没有那么先进。但是谢谢你的帮助。我会及时弄明白的。再次感谢您。NumPy的文档非常好,因此我建议您只需阅读loadtxt(函数)和dtype(概念)的文档。从这里开始,只需调用数组的sort()方法,按任何命名列进行就地排序。使用NumPy的主要优点是它有用C实现的数组操作,可以非常非常快。你的意思是在append
调用中有另一对括号吗?@user2357112:哦,当然!谢谢看到了吗?我对python已经拥有的“.sort()”是一个全新的概念,但在我尝试它时,我认为它不会起作用,但我认为我没有正确地分割行,所以它不起作用,这就是为什么我通过我在上面的代码中显示的路径尝试它。另外,我想说非常感谢您的帮助,也感谢您解释所采取的步骤。您的意思是在追加呼叫中再加一对括号吗?@user2357112:哦,当然!谢谢看到了吗?我对python已经拥有的“.sort()”是一个全新的概念,但在我尝试它时,我认为它不会起作用,但我认为我没有正确地分割行,所以它不起作用,这就是为什么我通过我在上面的代码中显示的路径尝试它。此外,我想对你的帮助表示非常感谢,并感谢你解释所采取的步骤。