Sorting 使用write对python创建的列表文件进行排序

Sorting 使用write对python创建的列表文件进行排序,sorting,python-3.x,Sorting,Python 3.x,我有一个由python3创建的文件,它使用: of.write("{:<6f} {:<10f} {:<18f} {:<10f}\n" .format((betah), test, (torque*13605.698066), (mom))) 现在,我想对列表进行排序 of.write("{:<6f} {:<10f} {:<18f} {:<10f}\n"

我有一个由python3创建的文件,它使用:

   of.write("{:<6f} {:<10f} {:<18f} {:<10f}\n"
                     .format((betah), test, (torque*13605.698066), (mom)))
现在,我想对列表进行排序

   of.write("{:<6f} {:<10f} {:<18f} {:<10f}\n"
                     .format((betah), test, (torque*13605.698066), (mom)))
分拣的预期输出为:

   of.write("{:<6f} {:<10f} {:<18f} {:<10f}\n"
                     .format((betah), test, (torque*13605.698066), (mom)))
25.0     29.07    0.143582198404     0.96      
20.0     35.95    0.220373446813     0.95   
15.0     47.13    0.0594315908872    0.933333333334
5.0     124.12    0.230837577743     0.800090803982
4.0     146.71    0.239706979471     0.750671150402
1.0     250.20    0.240498520899     0.313035285499
0.5     263.24    0.239785533064     0.163953413739
我在示例中尝试了和元组,但它们的输出如下所示

   of.write("{:<6f} {:<10f} {:<18f} {:<10f}\n"
                     .format((betah), test, (torque*13605.698066), (mom)))
['0.500000 263.240000 0.239786           0.163953  \n',  '15.000000 47.130000  0.059432           0.933333  \n', '1.000000 250.200000 0.240499           0.313035  \n',  '25.000000 29.070000  0.143582           0.960000  \n', '20.000000 35.950000  0.220373           0.950000  \n',  '4.000000 146.710000 0.239707           0.750671  \n',  '5.000000 124.120000 0.230838           0.800091  \n']
请不要试图匹配输入和输出的数量,因为为了简洁起见,它们都被截断了

   of.write("{:<6f} {:<10f} {:<18f} {:<10f}\n"
                     .format((betah), test, (torque*13605.698066), (mom)))
作为我自己的一个例子,我尝试在帮助下进行排序,如下所示:

   of.write("{:<6f} {:<10f} {:<18f} {:<10f}\n"
                     .format((betah), test, (torque*13605.698066), (mom)))

请帮助我正确排序文件。

您发现的问题是字符串是按字母顺序排序的,而不是按数字排序的。您需要做的是将每个项从字符串转换为浮点,对浮点列表进行排序,然后再次作为字符串输出

   of.write("{:<6f} {:<10f} {:<18f} {:<10f}\n"
                     .format((betah), test, (torque*13605.698066), (mom)))
我在这里重新创建了您的文件,因此您可以看到我正在直接从文件中读取

   of.write("{:<6f} {:<10f} {:<18f} {:<10f}\n"
                     .format((betah), test, (torque*13605.698066), (mom)))
pout = [
"15.0     47.13    0.0594315908872    0.933333333334",
"25.0     29.07    0.143582198404     0.96          ",
"20.0     35.95    0.220373446813     0.95          ",
"5.0     124.12    0.230837577743     0.800090803982",
"4.0     146.71    0.239706979471     0.750671150402",
"0.5     263.24    0.239785533064     0.163953413739",
"1.0     250.20    0.240498520899     0.313035285499"]

with open('test.txt', 'w') as thefile:
    for item in pout:
        thefile.write(str("{}\n".format(item)))

# Read in the file, stripping each line
lines = [line.strip() for line in open('test.txt')]
acc = []
# Loop through the list of lines, splitting the numbers at the whitespace
for strings in lines:
    words = strings.split()
    # Convert each item to a float
    words = [float(word) for word in words]
    acc.append(words)
# Sort the new list, reversing because you want highest numbers first
lines = sorted(acc, reverse=True)
# Save it to the file.
with open('test.txt', 'w') as thefile:
    for item in lines:
        thefile.write("{:<6} {:<10} {:<18} {:<10}\n".format(item[0], item[1], item[2], item[3]))
还要注意,我使用open'test.txt',w'作为文件:因为它会自动处理所有打开和关闭操作。更安全的内存。

帮助排序和导入操作符;helpoperator.itemgetter是一个很好的起点。
   of.write("{:<6f} {:<10f} {:<18f} {:<10f}\n"
                     .format((betah), test, (torque*13605.698066), (mom)))