【问题标题】:How do I pair the output from a dictionary to print the values with same key? [closed]如何将字典的输出配对以打印具有相同键的值? [关闭]
【发布时间】:2021-12-15 08:22:20
【问题描述】:

我正在尝试将输出中的值配对。我正在尝试将这些值与同一个键配对。 代码是

    key = input("parent_id:")
    value = input("child_id:")
    myList[key] = [value]
    myList

输出是

    myList = [{'parent_id' : 123, 'child_id' : 987},
    {'parent_id' : 234, 'child_id' : 876},
    {'parent_id' : 123, 'child_id' : 765},
    {'parent_id' : 345, 'child_id' : 654},
    {'parent_id' : 345, 'child_id' : 543}]

我希望输出是:

{ 123 : [987, 765],
 234 : [876],
 345 : [654, 543] }

我该怎么做?

【问题讨论】:

  • 试试collections.defaultdict
  • 很不清楚你想对输入做什么?
  • 一个类似的问题在这里已经有了答案:stackoverflow.com/q/69776880/10237506
  • @Jab 他们似乎想将'child_id''parent_id' 分组,尽管现在看这个有点不清楚他们是如何获得特定输出而不是预期的(或接近预期的(没有列表) ),OP需要提供完整的minimal reproducible example

标签: python dictionary key key-value ordereddictionary


【解决方案1】:

也许这就是你可以尝试的 -

from collections import defaultdict
out  = defaultdict(list)

myList = [{'parent_id' : 123, 'child_id' : 987},
    {'parent_id' : 234, 'child_id' : 876},
    {'parent_id' : 123, 'child_id' : 765},
    {'parent_id' : 345, 'child_id' : 654},
    {'parent_id' : 345, 'child_id' : 543}]

for item in myList:
    #print(item)                # item is a dict. 
    #print(item.keys(), item.values())
    k = item['parent_id']
    v = item['child_id']
    
    out[k].append(v)

运行它:

print(out)       #  matching expected output

【讨论】:

    【解决方案2】:

    你可以使用collections.defaultdict:

    from collections import defaultdict
    
    my_list = [{'parent_id' : 123, 'child_id' : 987},
    {'parent_id' : 234, 'child_id' : 876},
    {'parent_id' : 123, 'child_id' : 765},
    {'parent_id' : 345, 'child_id' : 654},
    {'parent_id' : 345, 'child_id' : 543}]
    
    result = defaultdict(list)
    
    for d in my_list:
        result[d['parent_id']].append(d['child_id'])
    

    如果你可以盲目地假设键和值都是相同的顺序并且总是有相同的键,那就更简单了:

    for key, val in map(dict.values, my_list):
        result[key].append(val)
    

    或者您可以使用itertools.groupby,但您需要先对数据进行排序:

    from itertools import groupby
    from operator import itemgetter
    
    get_parent = itemgetter('parent_id')
    get_child = itemgetter('child_id')
    sorted_list = sorted(my_list, key=get_parent)
    
    result = {k: list(map(get_child, g)) for k, g in groupby(sorted_list, get_parent)}
    

    两者的结果:

    {123: [987, 765], 234: [876], 345: [654, 543]}
    

    【讨论】:

      【解决方案3】:

      这个解决方案应该有效:

      myList = [{'parent_id': 123, 'child_id': 987},
                {'parent_id': 234, 'child_id': 876},
                {'parent_id': 123, 'child_id': 765},
                {'parent_id': 345, 'child_id': 654},
                {'parent_id': 345, 'child_id': 543}]
      
      return_dict = {}
      
      for item in myList:
          key = item.get("parent_id")
          value = item.get("child_id")
          if not return_dict.get(key):
              return_dict[key] = []
          return_dict[key].append(value)
      
      print(return_dict)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多