iT邦幫忙

1

python 串列

  • 分享至 

  • xImage

請問如果要排名到最後一個項(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)

ccutmis iT邦高手 2 級 ‧ 2023-03-08 11:57:54 檢舉
https://github.com/ryanhanwu/How-To-Ask-Questions-The-Smart-Way#readme
froce iT邦大師 1 級 ‧ 2023-03-08 12:39:21 檢舉
要依總分排序?

print(sorted(s, key=lambda x:sum(x[1:]), reverse=True))
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

1
kennex_x
iT邦新手 4 級 ‧ 2023-03-08 13:06:40

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)]
1
mackuo
iT邦研究生 2 級 ‧ 2023-03-08 15:16:18
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

https://ithelp.ithome.com.tw/upload/images/20230308/20122335vvGqp4Eud4.jpg

df['Sum'] = df.iloc[:,1:5].sum(axis=1)
df

https://ithelp.ithome.com.tw/upload/images/20230308/20122335Mb7rDzvbeB.jpg

# 排名
df.sort_values('Sum', ascending=False)

https://ithelp.ithome.com.tw/upload/images/20230308/20122335O05yDPSEyy.jpg

我要發表回答

立即登入回答