Python 3.4.3-按字母顺序排列统计列表
我有一个文本文件,显示了一个示例名称列表,后面是他们在算术测试中获得的分数Python 3.4.3-按字母顺序排列统计列表,python,sorting,python-3.4,Python,Sorting,Python 3.4,我有一个文本文件,显示了一个示例名称列表,后面是他们在算术测试中获得的分数 Aaa = 10 Ccc = 9 Ddd = 1 Bbb = 5 在另一个脚本中,我需要能够选择一个选项以按字母顺序对列表进行排序,并在向列表中添加更多名称后保持该选项,如下所示: Aaa = 10 Bbb = 5 Ccc = 9 Ddd = 1 通过这种方式,姓名按字母顺序排列,并与其原始分数相邻。我已经尝试了排序功能 f=open('score.txt','r') readfile=str(f.readlin
Aaa = 10
Ccc = 9
Ddd = 1
Bbb = 5
在另一个脚本中,我需要能够选择一个选项以按字母顺序对列表进行排序,并在向列表中添加更多名称后保持该选项,如下所示:
Aaa = 10
Bbb = 5
Ccc = 9
Ddd = 1
通过这种方式,姓名按字母顺序排列,并与其原始分数相邻。我已经尝试了排序
功能
f=open('score.txt','r')
readfile=str(f.readlines())
readfile.rstrip('\n')
print(sorted(readfile))
但是,这仅对文件的每个字符进行排序并返回以下内容:
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', "'", "'", "'", "'", "'", "'", "'", "'", ',', ',', ',', '0', '1', '1', '5', '9', '=', '=', '=', '=', 'A', 'B', 'C', 'D', '[', '\\', '\\', '\\', '\\', ']', 'a', 'a', 'b', 'b', 'c', 'c', 'd', 'd', 'n', 'n', 'n', 'n']
您正在使用
str(f.readlines())
创建从readlines返回的列表的字符串表示形式,因此调用sorted on将对每个字符进行排序,只需调用file对象上的sorted:
with open("in.txt") as f:
print(sorted(f))
with open('score.txt') as infile:
readfile = sorted(infile)
print(readfile)
输出:
['Aaa = 10\n', 'Bbb = 5', 'Ccc = 9\n', 'Ddd = 1\n']
['Aaa = 10', 'Bbb = 5', 'Ccc = 9', 'Ddd = 1']
['Aaa = 10\n', 'Bar = 12\n', 'Bbb = 5\n', 'Ccc = 9\n', 'Ddd = 1\n']
要删除换行符,请使用映射:
with open("in.txt") as f:
lines = map(str.rstrip,sorted(f))
print(lines)
输出:
['Aaa = 10\n', 'Bbb = 5', 'Ccc = 9\n', 'Ddd = 1\n']
['Aaa = 10', 'Bbb = 5', 'Ccc = 9', 'Ddd = 1']
['Aaa = 10\n', 'Bar = 12\n', 'Bbb = 5\n', 'Ccc = 9\n', 'Ddd = 1\n']
基本上,代码中发生了什么:
In [4]: l = ["1","2","3","4"] # readlines list
In [5]: str(l) # you turn it into a str
Out[5]: '[1, 2, 3, 4]'
In [6]: sorted(str(l)) # calling sorted sorts the individual characters
Out[6]: [' ', ' ', ' ', ',', ',', ',', '1', '2', '3', '4', '[', ']']
一旦您有了一个已排序的文件,要将新名称放在正确的位置,您可以使用tempfile写入并用shutil.move替换原始文件:
new_n, new_s = "Bar",12
from shutil import move
from tempfile import NamedTemporaryFile
with open("in.txt") as f, NamedTemporaryFile("w",dir=".",delete=False) as t:
for line in f:
if line >= new_n:
t.write("{} = {}\n".format(new_n, new_s))
t.write(line)
t.writelines(f)
break
t.write(line)
else:
t.write("{} = {}\n".format(new_n, new_s))
move(t.name,"in.txt")
最初排序并在.txt中写入后:
Aaa = 10
Bbb = 5
Ccc = 9
Ddd = 1
运行代码后,在.txt中:
Aaa = 10
Bar = 12
Bbb = 5
Ccc = 9
Ddd = 1
如果下次运行时添加“Foo”:
执行else,因为我们没有发现大于或等于foo的行/名称
如果您有一个已排序的列表,并且希望将新数据插入列表并保持顺序,则可以使用对分模块:
new_n, new_s = "Bar",12
from bisect import insort
with open("in.txt") as f:
lines = sorted(f)
insort(lines,"{} = {}".format(new_n, new_s))
print(lines)
输出:
['Aaa = 10\n', 'Bbb = 5', 'Ccc = 9\n', 'Ddd = 1\n']
['Aaa = 10', 'Bbb = 5', 'Ccc = 9', 'Ddd = 1']
['Aaa = 10\n', 'Bar = 12\n', 'Bbb = 5\n', 'Ccc = 9\n', 'Ddd = 1\n']
您没有列表,只有一个字符串: 这将列表转换为字符串,包括原始行之间的
[
,]
开始和结束以及,
逗号
不要这样做,这里绝对不需要将字符串列表转换为字符串。只需对你读到的行进行排序:
readfile = f.readlines()
print(sorted(readfile))
您甚至不需要在此处调用f.readlines()
,因为sorted()
可以接受任何iterable,包括文件对象:
with open("in.txt") as f:
print(sorted(f))
with open('score.txt') as infile:
readfile = sorted(infile)
print(readfile)
您需要考虑的是,如果文件中有任何重复的名称,它们将按照
=
字符后面的数字按字典顺序排序,而不是按数字排序。这意味着100
排序在9
之前,因为在Unicode标准中1
位于9
之前。为什么要将读取的行列表转换为字符串?删除您的str()
调用。如果你想再写一次,你想在换行符中留下;在任何情况下,您的readfile.rstrip()
调用都是无用的,因为您a)忽略结果,b)在readfile
末尾没有换行符,因为它以]
结尾。如果您单独使用了f.readlines()
,则排序()
可以很好地工作,因为您有一个要排序的字符串列表。您可以去掉白色spaces@VigneshKalai,我认为OP只是想对内容进行最初的排序,然后在添加新名称时保持其有序性。谢谢,这很有帮助。我还使用了这个readfile=[w.replace('\n','')代表readfile中的w]
来消除输出中的\n。@HelpMeWithMyCode:然后使用[w.rstrip('\n')代表readfile中的w]
仅从末尾删除它们(您仍然可以找到换行符的唯一位置)。