例如,如果这是文件...
Batman, Bruce Wayne, high tech weapons, 70
Spiderman, Peter Parker, wrist web-shooter, 70
Superman, Clark Kent, superhuman strength, 99
Captain America, Steve Rogers, vibranium-steel shield, 71
Wonder Woman, Princess Diana, indestructible bracelets, 80
Iron Man, Tony Stark, powered armor suit, 85
Donald Knuth, Donald Knuth, computer science and mathematics, 100
Hulk, Robert Banner, superhuman strength, 90
He-Man, Prince Adam, superhuman strength, 92
Wolverine, Logan, retractable bone claws, 70
我被要求根据超级英雄的权力(每行末尾的数字)按升序排列......
帮帮我吧!
答案 0 :(得分:0)
s = """Batman, Bruce Wayne, high tech weapons, 70
Spiderman, Peter Parker, wrist web-shooter, 70
Superman, Clark Kent, superhuman strength, 99
Captain America, Steve Rogers, vibranium-steel shield, 71
Wonder Woman, Princess Diana, indestructible bracelets, 80
Iron Man, Tony Stark, powered armor suit, 85
Donald Knuth, Donald Knuth, computer science and mathematics, 100
Hulk, Robert Banner, superhuman strength, 90
He-Man, Prince Adam, superhuman strength, 92
Wolverine, Logan, retractable bone claws, 70"""
ss = s
s = s.split('\n')
x = [e.split(',') for e in s]
x = sorted(x, key=lambda x:int(x[-1].strip()))
s = '\n'.join([','.join(e) for e in x])
你去吧
答案 1 :(得分:0)
也发布了一个大熊猫的答案:
s = u"""Batman, Bruce Wayne, high tech weapons, 70
Spiderman, Peter Parker, wrist web-shooter, 70
Superman, Clark Kent, superhuman strength, 99
Captain America, Steve Rogers, vibranium-steel shield, 71
Wonder Woman, Princess Diana, indestructible bracelets, 80
Iron Man, Tony Stark, powered armor suit, 85
Donald Knuth, Donald Knuth, computer science and mathematics, 100
Hulk, Robert Banner, superhuman strength, 90
He-Man, Prince Adam, superhuman strength, 92
Wolverine, Logan, retractable bone claws, 70"""
import io
import pandas as pd
df = pd.read_csv(io.StringIO(s),names=["Hero","Name","Special","Power"],skipinitialspace=True)
df.sort_values("Power",ascending=True, inplace=True)
df = df.reset_index(drop=True)
#df.to_dict(orient="r")
#df.to_csv("output.csv",index=False)
output = '\n'.join([', '.join([str(i) for i in row]) for row in df.values])
print(output)
答案 2 :(得分:0)
这是一个相当简单的问题,但只需几步。搜索“如何将.txt文件加载到python中?”之类的问题。会引导你使用pandas模块,以及“如何按列对pandas DataFrame进行排序?”会引导您使用sort_values DataFrame方法。如果您无法自己找到问题的答案,则应将其分解为更小的部分。有关如何执行这些步骤的答案已在堆栈溢出的其他答案中提供。
虽然仍然有用,但这是你要使用的方法。 我假设这些数据存储为.txt或.csv文件。
使用pandas将数据读入数据帧,然后按Power列降序排序。您可以将数据帧输出为.csv,excel,.txt或您想要的任何文件类型。根据您想要的文件类型,该步骤的堆栈溢出有很多答案。
import pandas as pd
df = pd.read_csv('sort_input.txt', delimiter=',', header=None,
names=['Alias','Name','Skill','Power'])
df.sort_values(['Power'], ascending=False, inplace=True)
print(df)
给出输出:
Donald Knuth, Donald Knuth, computer science and mathematics,100
Superman, Clark Kent, superhuman strength,99
He-Man, Prince Adam, superhuman strength,92
Hulk, Robert Banner, superhuman strength,90
Wonder Woman, Princess Diana, indestructible bracelets,80
Captain America, Steve Rogers, vibranium-steel shield,71
tman, Bruce Wayne, high tech weapons,70
Spiderman, Peter Parker, wrist web-shooter,70