[英]How to combine values in two different dictionaries that have the same key in python
I was given two files. 我被给了两个文件。 file1 contains a list of country names and their continent such as: file1包含国家/地区名称及其大陆的列表,例如:
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: 和file2包含国家/地区名称及其人口和区域的列表,例如:
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. 我想创建两个词典,一个用于file1,一个用于file2,然后我想组合所有具有相同键的值。 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]]
Ok, so assuming you have populated the two dictionaries: Continent, Information 好的,假设您填写了两个词典: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:
然后:
Information = {'china': [193364000, 8511965.00, 'Asia']}
would give: 会给:
Information = {'china': [193364000, 8511965.00, 'Asia']}
Hope this helps. 希望这可以帮助。
如果您可以保证密钥相同,则可以使用字典理解:
{country:Information[country] + [Continent[country]] for country in Continent}
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()}
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. 假设人口和区域数据作为列表值存储在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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.