简体   繁体   English

从字典列表中创建每个键具有多个值的字典

[英]Creating a dictionary with multiple values per key from a list of dictionaries

I have the following list of dictionaries: 我有以下词典列表:

listofdics = [{'StrId': 11, 'ProjId': 1},{'StrId': 11,'ProjId': 2},
              {'StrId': 22, 'ProjId': 3},{'StrId': 22, 'ProjId': 4},
              {'StrId': 33, 'ProjId': 5},{'StrId': 33, 'ProjId': 6},
              {'StrId': 34, 'ProjId': 7}]

I need to get all ProjId values for StrId that are duplicate. 我需要把所有ProjId的值StrId是重复的。 So this is the output I'm looking for: 所以这是我正在寻找的输出:

new_listofdics = [{11:[1,2]}, {22:[3,4]}, {33:[5,6]], {34:[7]}]

I wrote a function that creates a list of dictionaries with StrId values as keys, and a list with all ProjId that share the same key as values. 我编写了一个函数,该函数创建一个将StrId值作为键的字典列表,以及一个将所有ProjId与值共享相同键的列表。 Here it is: 这里是:

def compare_projids(listofdics):
    proj_ids_dups = {} 

    for row in listofdics:       
        id_value = row['StrId']
        proj_id_value = row['ProjId']
        proj_ids_dups[id_value]=proj_id_value

        if row['StrId'] == id_value:
            sum_ids = []
            sum_ids.append(proj_id_value)  
        proj_ids_dups[id_value]=sum_ids
     return proj_ids_dups

This is the output I get now: 这是我现在得到的输出:

new_listofdics=  {33: [6], 34: [7], 11: [2], 22: [4]}

What I see is that append replaces each ProjId value with the last one iterated, instead of adding them at the end of the list. 我看到的是, append用迭代的最后一个替换每个ProjId值,而不是将它们添加到列表的末尾。

How can I fix this?... 我怎样才能解决这个问题?...

It's unclear why you need to have such output new_listofdics = [{11:[1,2]}, {22:[3,4]}, {33:[5,6]], {34:[7]}] , because it's better to have just dict object. 目前尚不清楚为什么需要这样的输出new_listofdics = [{11:[1,2]}, {22:[3,4]}, {33:[5,6]], {34:[7]}] ,因为最好只有dict对象。

So program would look like this 所以程序看起来像这样

>>> from collections import defaultdict
>>> listofdics = [{'StrId': 11, 'ProjId': 1},{'StrId': 11,'ProjId': 2},
              {'StrId': 22, 'ProjId': 3},{'StrId': 22, 'ProjId': 4},
              {'StrId': 33, 'ProjId': 5},{'StrId': 33, 'ProjId': 6},
              {'StrId': 34, 'ProjId': 7}]
>>> output = defaultdict(list)
>>> for item in listofdics:
...     output[item.get('StrId')].append(item.get('ProjId'))
>>> dict(output)
{11: [1, 2], 22: [3, 4], 33: [5, 6], 34: [7]}

It's much easier to go through that dict that your desired output. 通过该命令获得所需的输出要容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每个键具有多个值的字典列表为 Dataframe - List of dictionaries with multiple values per key as Dataframe 从 dataframe 中的特定列创建每个键具有多个值的字典 - Creating a dictionary with multiple values per key from specific columns in a dataframe 列表到字典转换,每个键有多个值? - list to dictionary conversion with multiple values per key? 列出每个键具有多个值的字典 - list to dictionary with multiple values per key 编写字典列表,每个键有多个值作为新行 - Writing a list of dictionaries, with multiple values per key as new rows 从每个键具有多个值的字典将数据写入csv - writing data to csv from dictionaries with multiple values per key 如果与列表中每个字典中的一个键的值匹配,则将具有不同词典值的新键添加到词典列表中 - Add new key to list of dictionaries with the values from a different dictionaries if matched with value of one key in each dictionary in a list 转置字典(从字典列表中提取一个键的所有值) - transpose dictionary (extract all the values for one key from a list of dictionaries) 使用键和值转换字典中的字典列表 - Converting a list of dictionaries in a dictionary using key and values 用字典而不是python中的多个字典创建列表 - Creating list with dictionary instead of multiple dictionaries in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM