简体   繁体   English

如何在python中具有相同键的两个不同字典中组合值

[英]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.

相关问题 组合具有相同键名的 Python 字典 - Combine Python dictionaries that have the same Key name 在python中将相同的嵌套字典与不同的值结合起来 - combine same of nested dictionaries with different values in python 在Python中将2个具有相同键但值不同的字典组合在一起 - Combine 2 dictionaries with the same keys but different values in Python 如何在 python 中组合(附加值)两个具有相同键的嵌套字典? - How to combine (append values) two nested dictionaries with the same keys in python? 通过识别具有相同键但具有不同值的集合来比较Python中的两个词典 - Comparing two dictionaries in Python by identifying sets with same key but different values Python:检查字典中具有不同键的两个字典的相同值 - Python: check same values in the dictionary for two dictionaries with different key 将两个具有相同键但不同字典的嵌套字典组合为值 - Combine two nested dictionaries with same keys but different dictionaries as values 用相同的键减去两个 python 字典的值 - subtract values of two python dictionaries with the same key 如何比较 python 中具有不同键的两个字典的值 - How to compare values of two dictionaries that have different keys in python 如何为字典中的相同键生成不同的值? - How to generate different values for same key in dictionaries?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM