4 回答
TA贡献2080条经验 获得超4个赞
简短的使用方法:)
import pandas as pd
df = pd.read_table('leaderboard.txt', delimiter=":", names=('scor', 'Name'))
df.sort_values(by='scor', inplace=True)
df.head()
scor Name
7 96 Paul
3 92 Avi
8 80 Pauline
0 76 Daniel
9 72 Fran
6 54 Mordechai
5 46 Jeremy
1 36 Gabriel
2 30 Perry
4 28 Yehuda
arr = df.values
TA贡献1868条经验 获得超4个赞
发生这种情况是因为您正在比较诸如此类的项目if 76: Daniel>36: Gabriel 您可以尝试这样的事情
mylist=['76: Daniel','36: Gabriel','30: Perry','92: Avi','28: Yehuda','46: Jeremy','54: Mordechai','96: Paul','80: Pauline','72: Fran']
def bubbleSort(arr):
n=len(arr)
for i in range(n-1):
for j in range(0, n-i-1):
if int(arr[j][:2]) < int(arr[j+1][:2]) :
arr[j], arr[j+1] = arr[j+1], arr[j]
bubbleSort(mylist)
print(mylist)
>>>['96: Paul', '92: Avi', '80: Pauline', '76: Daniel', '72: Fran', '54: Mordechai', '46: Jeremy', '36: Gabriel', '30: Perry', '28: Yehuda']
TA贡献1719条经验 获得超6个赞
import re
string1 = lines[0]
int(re.search(r'\d+', string1).group())
而不是 arr = int(float(lines[0])),尝试使用上面的那个。String1 包含整数值。在那上面应用冒泡排序。
添加回答
举报