請問如果要排名到最後一個項(1~4)
s=[['A',68,74,64,81,0,0,0],
['B',77,80,65,72,0,0,0],
['C',95,90,82,85,0,0,0],
['D',42,55,45,48,0,0,0]]
print(s)
for i in range(4):
s[i][5]=sum(s[i][1:5])
s[i][6]=s[i][5]/4
print(s)
code
s = [['A',68,74,64,81,0,0,0], ['B',77,80,65,72,0,0,0], ['C',95,90,82,85,0,0,0], ['D',42,55,45,48,0,0,0]]
max_u, max_s = "", 0
score_table = {}
for i in range(len(s)):
score = sum(s[i][1:])
score_table[s[i][0]] = score
print("{} total: {}".format(s[i][0], score))
if score >= max_s:
max_u, max_s = s[i][0], score
print("Highest: {}, Total: {}".format(max_u, max_s))
print("Rank: {}".format(sorted(score_table.items(), key = lambda item : item[1], reverse = True)))
result
A total: 287
B total: 294
C total: 352
D total: 190
Highest: C, Total: 352
Rank: [('C', 352), ('B', 294), ('A', 287), ('D', 190)]
import pandas as pd
s=[['A',68,74,64,81,0,0,0],
['B',77,80,65,72,0,0,0],
['C',95,90,82,85,0,0,0],
['D',42,55,45,48,0,0,0]]
df = pd.DataFrame(s)
df
df['Sum'] = df.iloc[:,1:5].sum(axis=1)
df
# 排名
df.sort_values('Sum', ascending=False)