Lists within lists vs dictionaries

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ELK
    New Member
    • Feb 2008
    • 2

    Lists within lists vs dictionaries

    I recently started a beginners programming course and am struggling with my latest assignment. I have been given a .txt file of the results of a relay race with the following columns of data:
    team
    leg
    runner
    time

    It is my job to write a code which reads this file and outputs a list of the teams with their total times. I have had no issues importing the file, but I am completely lost on how to link team names (which are not specified) with the times of each runner (3 to a team) - let alone how to get the total 3 times from seconds to hh:mm:ss

    I know that this can be solved with a dictionary but can't find anything on dictionaries and file input that seems to help. I assumed a simpler method would be to have two seperate lists or a list within a list such as
    TIME = [[N1, T1], [N2, T2]....]
    but I simply get an error code saying the above names are not defined when I try the list within a list method.

    If anyone can see what the massive gap in my knowledge is then I would be extremely grateful, this one really has me stummped and I'm starting to get quite aggitated!
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by ELK
    I recently started a beginners programming course and am struggling with my latest assignment. I have been given a .txt file of the results of a relay race with the following columns of data:
    team
    leg
    runner
    time

    It is my job to write a code which reads this file and outputs a list of the teams with their total times. I have had no issues importing the file, but I am completely lost on how to link team names (which are not specified) with the times of each runner (3 to a team) - let alone how to get the total 3 times from seconds to hh:mm:ss

    I know that this can be solved with a dictionary but can't find anything on dictionaries and file input that seems to help. I assumed a simpler method would be to have two seperate lists or a list within a list such as
    TIME = [[N1, T1], [N2, T2]....]
    but I simply get an error code saying the above names are not defined when I try the list within a list method.

    If anyone can see what the massive gap in my knowledge is then I would be extremely grateful, this one really has me stummped and I'm starting to get quite aggitated!
    Column 1 in your file is not the team name? A dictionary with the team names as the keys would be a good start. You could tabulate the team data as a subdictionary with the runner names as the keys.
    The HH:MM:SS can be calculated from the seconds and formatted using the string formatting operator. [code=Python]''.join([outStr, '%02d:%02d:%02d ' % (hours, minutes, seconds)])[/code]All you need to do is calculate the hours, minutes and seconds which is straightforward .

    The dictionary could be organized like this:[code=Python]
    {'team1': {'runner1':[time1, time2, time3]}, {'runner2':[time1,....
    'team2': {'runner1':[.......[/code]
    [code=Python]
    for team in teamdict:
    for runner in teamdict[team]:
    print '%s total time is %s' % (runner, conv_seconds(su m(teamdict[team][runner])))[/code]where conv_seconds is a function that converts and returns the total seconds to HH:MM:SS format.
    HTH

    Comment

    • ELK
      New Member
      • Feb 2008
      • 2

      #3
      thank you so much for getting back so quickly. the one thing I managed to miss out however was that the number of teams entering the race is undisclosed and I assume this makes a difference to the code? an example of the results file we've been given is also below.

      TEAM,LEG,RUNNER ,TIME
      Team C,1,Runner 3,914
      Team A,1,Runner 1,1049
      Team A,2,Runner 4,1109
      Team B,1,Runner 2,1132
      Team B,3,Runner 8,1132
      Team C,3,Runner 9,1154
      Team A,3,Runner 7,1169
      Team C,2,Runner 6,1714
      Team B,2,Runner 5,1130

      thanks again :)

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by ELK
        thank you so much for getting back so quickly. the one thing I managed to miss out however was that the number of teams entering the race is undisclosed and I assume this makes a difference to the code? an example of the results file we've been given is also below.

        TEAM,LEG,RUNNER ,TIME
        Team C,1,Runner 3,914
        Team A,1,Runner 1,1049
        Team A,2,Runner 4,1109
        Team B,1,Runner 2,1132
        Team B,3,Runner 8,1132
        Team C,3,Runner 9,1154
        Team A,3,Runner 7,1169
        Team C,2,Runner 6,1714
        Team B,2,Runner 5,1130

        thanks again :)
        The number of teams and number of runners would make no difference to the code. You will build the dictionary as you iterate on the data.

        Comment

        Working...