按字æ¯é¡ºåºï¼Œä»Žæœ€é«˜åˆ°æœ€ä½Žå’Œå¹³å‡å€¼å¯¹csv中的数æ®è¿›è¡ŒæŽ’åº

时间:2016-05-01 14:42:50

标签: python sorting csv average

这是我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']]

因此,如果这是我读å–æ•°æ®çš„æ ¼å¼ï¼Œæˆ‘将如何读å–文件中的é‡å¤å称,如果它们在这样的数组中则添加分数?

1 个答案:

答案 0 :(得分:1)

第一步是将问题分解为å°æ­¥éª¤ï¼š

  1. How to open and handle the file(使用该部分底部的with声明)
  2. How to traverse a csv file
  3. How to sort the entries
  4. How to sort by the second value of each row
  5. How to print each element of a list on a separate line
  6. How to count total scores
  7. 扩展到最åŽä¸€ä¸ªï¼Œä½ å¯ä»¥æ€»è®¡å¾—分以åŠæ¯ä¸ªåå­—çš„æ¡ç›®æ•°ï¼Œå¦‚下所示:

    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)