这是我currently unresolved question的下一æ¥ï¼Œæˆ‘试图从3个ä¸åŒçš„团队ä¸åˆ†æ•°ã€‚我对python的知识éžå¸¸æœ‰é™ï¼Œå› 为我是编程新手,所以我解决这个当å‰é¡¹ç›®çš„问题éžå¸¸å›°éš¾ã€‚
首先,我将需è¦ç¤ºä¾‹æ•°æ®ï¼ˆå¦‚下所示),它们分为两个å•å…ƒæ ¼ï¼Œæ ¹æ®å称按å—æ¯é¡ºåºæŽ’åºï¼Œæˆ‘将在3个ä¸åŒçš„文件ä¸ä¸º3个ä¸åŒçš„团队æä¾›æ¤æ•°æ®ã€‚æˆ‘ä¹Ÿè¯•å›¾æ ¹æ®å¾—分从最高到最低排åºï¼Œè¿™å¯¹æˆ‘æ¥è¯´å·²ç»è¯æ˜Žäº†å¾ˆå¤šå›°éš¾ã€‚
Jake,5
Jake,3
Jake,7
Jeff,6
Jeff,4
Fred,5
我想è¦åšçš„第三ç§ä¹Ÿæ˜¯æœ€åŽä¸€ç§æŽ’åºæ–¹å¼æ˜¯å¹³å‡å€¼ã€‚为æ¤æˆ‘è¯•å›¾è¿™æ ·åšï¼Œå¦‚果用户有2或3次åç§°ï¼ˆå› ä¸ºè¯¥ç¨‹åºå°†å˜å‚¨æ¯ä¸ªç”¨æˆ·çš„最åŽ3个分数,这是一个currently unresolved问题)然åŽå®ƒä¼šæ·»åŠ 他们的分数然åŽé™¤ä»¥é‚£é‡Œæœ‰å¤šå°‘人。ä¸å¹¸çš„是,这对我æ¥è¯´éžå¸¸å›°éš¾ï¼Œæˆ‘很难获得任何输出,但我知é“这会将他们的平å‡åˆ†æ•°æ‰“å°åˆ°ä¸€ä¸ªå•ç‹¬çš„文件然åŽé‡æ–°è¯»å–分数。
到目å‰ä¸ºæ¢ï¼Œæˆ‘ç›®å‰çš„布局如下所示:
admin_data = []
team_choice = input("Choose a team to sort")
if team_choice == 'Team 1':
path = 'team1scores.csv'
elif team_choice == 'Team 2':
path = 'team2scores.csv'
elif team_choice == 'Team 3':
path = 'team3scores.csv'
else:
print("--Error Defining File Path--")
print("As an admin you have access to sorting the data")
print("1 - Alpahbetical")
print("2 - Highest to Lowest")
print("3 - Average Score")
admin_int = int(input("Choose either 1, 2 or 3?"))
if sort_int == 1 and team_choice == 'Team 1':
do things
elif sort_int == 2 and team_choice == 'Team 1':
do things
elif sort_int == 3 and team_choice == 'Team 1':
do things
这部分程åºå°†ç”¨äºŽæ¯ä¸ªæ–‡ä»¶ï¼Œä½†æ˜¯æ²¡æœ‰è¿æ°”为我需è¦çš„æ¯ç§ä¸åŒçš„排åºæ–¹å¼ç”Ÿæˆä»»ä½•è§£å†³æ–¹æ¡ˆã€‚如果我的项目first partçš„ç”案也得到了回ç”,我也将ä¸èƒœæ„Ÿæ¿€ã€‚
编辑(16:43): 我已设法完æˆç¨‹åºçš„最高到最低部分,但æ£åœ¨æ‰“å°ï¼š
[['Fred', '9'], ['George', '7'], ['Jake', '5'], ['Jake', '4'], ['Derek', '4'], ['Jake', '2']]
å› æ¤ï¼Œå¦‚果这是我读å–æ•°æ®çš„æ ¼å¼ï¼Œæˆ‘将如何读å–文件ä¸çš„é‡å¤åç§°ï¼Œå¦‚æžœå®ƒä»¬åœ¨è¿™æ ·çš„æ•°ç»„ä¸åˆ™æ·»åŠ 分数?
ç”案 0 :(得分:1)
第一æ¥æ˜¯å°†é—®é¢˜åˆ†è§£ä¸ºå°æ¥éª¤ï¼š
with
声明)扩展到最åŽä¸€ä¸ªï¼Œä½ å¯ä»¥æ€»è®¡å¾—分以åŠæ¯ä¸ªåå—çš„æ¡ç›®æ•°ï¼Œå¦‚下所示:
import csv
import collections
...
with open(path) as f:
entries = collections.Counter()
total_scores = collections.Counter()
for name,score in csv.reader(f):
total_scores[name] += int(score)
entries[name] += 1
然åŽï¼Œæ‚¨å¯ä»¥ä½¿ç”¨total_scores[name] / entries[name]
for name in sorted(entries):
ave_score = total_scores[name] / entries[name]
print(name,ave_score) #sep=", ")
其他两个动作éžå¸¸ç®€å•ï¼Œä¸Šé¢åˆ—出了一些æ¥éª¤ã€‚
import csv
import collections
from operator import itemgetter
...
if sort_int == 1:
with open(path) as f:
reader = csv.reader(f)
for name, score in sorted(reader):
print(name,score)
elif sort_int == 2:
with open(path) as f:
entries = sorted(csv.reader(f),
key=itemgetter(1),
reverse=True)
for name,score in entries:
print(name,score)
elif sort_int == 3:
with open(path) as f:
entries = collections.Counter()
total_scores = collections.Counter()
for name,score in csv.reader(f):
score = int(score)
total_scores[name] += score
entries[name] += 1
for name in sorted(entries):
ave_score = total_scores[name] / entries[name]
print(name,ave_score)
如果您想è¦åº”用最高到最低的平å‡åˆ†æ•°ï¼Œé‚£ä¹ˆæ‚¨éœ€è¦å¼•ç”¨æ‰€æœ‰å¹³å‡å€¼ï¼Œä¾‹å¦‚dict
:
ave_scores = {}
for name in sorted(entries):
ave_score = total_scores[name] / entries[name]
ave_scores[name] = ave_score
for name,ave_score in sorted(ave_scores.items(), key = itemgetter(1), reversed=True):
print(name,ave_score)