Hi, I'm processing a small text file which contains information about the Winter Olympics. However I don't know how to keep the list the same for each country whilst making a new list for a different country.

So far I've read in the lines and I have put the data the following code:

country = ["Germany","Germany","Italy"]  #e.g German gets Gold and Silver and Italy gets a bronze
medals = ["Gold","Silver","Bronze"]
event = ["Luge men's single"]
x = []
temp = {}
data = {}


for i in range(len(country)):              
            x.append(medal[i])
            temp[event] = x
            data[countries[i]] = temp

The data structue I want to create is:

{"country":{"event name":[medals],... "event name2":[medals] } }

If I put x=[] and the end of the for loop, it just returns the last medal.

Recommended Answers

All 4 Replies

This will yield the correct results using your test data, but you will have to figure out how to get it to work with your input file.

country = ["Germany","France","Italy"]
medals = ["Gold","Silver","Bronze"]
event = ["Luge men's single"]

data = {}
for this_country in country:              
    data[this_country] = {}
    for this_event in event:
        data[this_country][this_event] = []
        for this_medal in medals:
            data[this_country][this_event].append(this_medal)
    ## delete one element so all countries are different
    del medals[0]
for this_country in country:
    print this_country, data[this_country]
"""  My results
Germany {"Luge men's single": ['Gold', 'Silver', 'Bronze']}
France {"Luge men's single": ['Silver', 'Bronze']}
Italy {"Luge men's single": ['Bronze']}
"""

Thanks, I got it to work with my text file, however I think I will have to change the code quite a bit.

Since the text file has various events I would like to keep it in the same dictionary, the above code changes the dictionary value every time.

e.g. I was trying to get:

{Germany: {"Luge men's single": ,"Luge women's doubles:[Bronze]}

I think I will be able to manage it, thanks though.

Also I think I should have explained the output better.

With the list country = ["Germany","Germany","Italy"] this means that the Germany medal list should contain ["Gold","Silver"] whilst for Italy it should just be ["Bronze"]

Since the data of the 2010 Olympics would be relatively small, you could go with a list of (event, country_gold, country_silver, country_bronze) tuples, as show in this example ...

# index constants
EVENT = 0
GOLD = 1
SILVER = 2
BRONZE = 3

# [(event, country_gold, country_silver, country_bronze), ...]
figure_skating = [
("Figure Skating Women", "Korea", "Japan", "Canada"),
("Figure Skating Men", "US", "Russia", "Japan"),
("Figure Skating Ice Dance", "Canada", "US", "Russia"),
("Figure Skating Pairs", "China", "China", "Germany")
]

alpine_skiing = [
("Downhill Women", "US", "US", "Austria"),
("Downhill Men", "Switzerland", "Norway", "US"),
("Super-G Women", "Norway", "Slovenia", "US"),
("Super-G Men", "Norway", "US", "US"),
("Giant Slalom Women", "Germany", "Slovenia", "Austria"),
("Giant Slalom Men", "Switzerland", "Norway", "Norway"),
("Slalom Women", "Germany", "Austria", "Czech Republic"),
("Slalom Men", "Italy", "Croatia", "Sweden"),
("Super Combined Women", "Germany", "US", "Sweden"),
("Super Combined Men", "US", "Croatia", "Switzerland")
]

# you can combine all categories you have
all_events = figure_skating + alpine_skiing

# for example country and gold medals
# create a country:number_gold dictionary
d_gold = {}
for line in all_events:
    d_gold[line[GOLD]] = d_gold.get(line[GOLD], 0) + 1


print(d_gold)

"""my output (prettified) -->
{
'Canada': 1,
'Korea': 1, 
'Italy': 1, 
'US': 3, 
'China': 1, 
'Switzerland': 2, 
'Germany': 3, 
'Norway': 2
}
"""

I decided to keep the original structure as I'm going to print the information. I've got it working now.

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.