3

I was given two files. file1 contains a list of country names and their continent such as:

Country,Continent
China,Asia
United States of America,North America
Brazil,South America

and file2 contains a list of country names and their population and areas, such as:

Country|Population|Area
China|1,339,190,000|9,596,960.00
United States of America|309,975,000|9,629,091.00
Brazil|193,364,000|8,511,965.00

I wanted to create two dictionaries, one for file1 and one for file2, then I wanted to combine all the values that has the same key. My final output should be something like this:

China:[193364000, 8511965.00, Asia]

Where the population is an integer and area is float

I made two dictionaries, however I have no idea how to combine the values together.

Here is my code:

#first dictionary
Continent = {}
file2 = open("file2.txt", "r") 
for i in file2:
  file2 = i.strip
  parts2 = i.split(",") 
  Continent[parts2[0]] = parts2[1] 


#Second dictionary
Information = {}
file1 = open("file1.txt", "r")
for j in file1:
  file1 = j.strip
  part1 = j.split("|")
#Strip "," off from the values so that it's an intger/float
  part1[1] = part1[1].replace(",", "") 
  part1[2] = part1[2].replace(",", "")
  Information[part1[0]] = [part1[1], part1[2]]
Qwert
  • 67
  • 6
  • 2
    Possible duplicate of [How to merge multiple dicts with same key?](https://stackoverflow.com/questions/5946236/how-to-merge-multiple-dicts-with-same-key) – Tom Karzes Nov 30 '17 at 21:40
  • How about changing your last row to `Information[part1[0]] = [part1[1], part1[2], Continent[parts1[0]]]` – DanielM Nov 30 '17 at 21:40
  • @style then it will just print the same continent no matter which country it is – Qwert Nov 30 '17 at 21:54
  • @Qwert why? `part1[0]` should be the current country.. – DanielM Nov 30 '17 at 21:56

4 Answers4

3

Ok, so assuming you have populated the two dictionaries: Continent, Information

Assuming that both dictionaries have the same keys, this would work:

let's assume:

Continent = {'china': 'Asia'}
Information = {'china': [193364000, 8511965.00]}
then:
for key in Continent.keys():
    Information[key]=Information[key] + [Continent[key]]

would give:

Information = {'china': [193364000, 8511965.00, 'Asia']}

Hope this helps.

  • does it matter if I use "for key in Information.kets()" instead of using Continent.keys? – Qwert Nov 30 '17 at 21:57
1

If you can guarantee that the keys are the same, you can use a dictionary comprehension:

{country:Information[country] + [Continent[country]] for country in Continent}
yinnonsanders
  • 1,831
  • 11
  • 28
1

You can try this:

continents = [i.strip('\n').split(',') for i in open('filename.txt')][1:-1]
country_data = [i.strip('\n').split('|') for i in open('filename.txt')][1:-1]
new_country_data = {a:[float(''.join(s.split(','))) for s in b]+[continents[a]] for a, b in country_data.items()}
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
1

You cannot concatenate string type of continent with list type of population & areas data. Assuming that population and area data is stored as list value in file2 dict. Try this:

dict1 = {"China":"Asia","United States of America":"North America","Brazil":"South America"}
dict2 = {"China":[1339190000,9596960.00],"United States of America":[309975000,9629091.00],"Brazil":[193364000,8511965.00]}
dict3 = {}

for country,continent in dict1.items():
    for country2 in dict2.keys():
        if country == country2:
            data = dict2[country2]
            data.append(continent)
            dict3.update({country:data})

print dict2
tejas619
  • 41
  • 4