473,467 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Lists within lists vs dictionaries

2 New Member
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!
Feb 24 '08 #1
3 1262
bvdet
2,851 Recognized Expert Moderator Specialist
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.
Expand|Select|Wrap|Line Numbers
  1. ''.join([outStr, '%02d:%02d:%02d' % (hours, minutes, seconds)])
All you need to do is calculate the hours, minutes and seconds which is straightforward.

The dictionary could be organized like this:
Expand|Select|Wrap|Line Numbers
  1. {'team1': {'runner1':[time1, time2, time3]}, {'runner2':[time1,....
  2.  'team2': {'runner1':[.......
Expand|Select|Wrap|Line Numbers
  1. for team in teamdict:
  2.     for runner in teamdict[team]:
  3.         print '%s total time is %s' % (runner, conv_seconds(sum(teamdict[team][runner])))
where conv_seconds is a function that converts and returns the total seconds to HH:MM:SS format.
HTH
Feb 24 '08 #2
ELK
2 New Member
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 :)
Feb 24 '08 #3
bvdet
2,851 Recognized Expert Moderator Specialist
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.
Feb 24 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Narendra C. Tulpule | last post by:
Hi, if you know the Python internals, here is a newbie question for you. If I have a list with 100 elements, each element being a long string, is it more efficient to maintain it as a dictionary...
11
by: Amy G | last post by:
I have received such good help on this message board. I wonder if I might not get a little more help from you on this. I am at the point where I have two dictionaries, with information of a...
2
by: Stewart Midwinter | last post by:
I would like to link the contents of three OptionMenu lists. When I select an item from the first list (call it continents), the contents of the 2nd list (call it countries) would update. And in...
1
by: Gabriel B. | last post by:
I just sent an email asking for hints on how to import data into a python program As i said earlier i'm really new to python and besides being confortable with the syntax, i'm not sure if i'm on...
10
by: Philippe C. Martin | last post by:
Hi, I'm looking for an easy algorithm - maybe Python can help: I start with X lists which intial sort is based on list #1. I want to reverse sort list #1 and have all other lists sorted...
9
by: Dave H | last post by:
Hello, I have a query regarding definition lists. Is it good practice semantically to use the dt and dd elements to mark up questions and answers in a frequently asked questions list, or FAQ? ...
5
by: Fox | last post by:
Hi, I am working on a project which used dictionaries. I am having to remake part of this and have no experience with the scripting dictionary. I need to see how to create multiple...
9
by: SMB | last post by:
I have two lists of data like the following: LIST1 , ] LIST2 , 'label': 'First Name', 'width': 0L, 'separator': ',', 'height': 0L, 'type': 2L, 'order': 1L}, {'code': 14L, 'name': 'Last...
5
by: Ladislav Andel | last post by:
Hi, I have a list of dictionaries. e.g. how could I make a new list of dictionaries which would look like: , 'service_domain': 'dp0.example.com'}, {'transports': , 'service_domain':...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.