【问题标题】:Python attach list of dict to another dict as new keyPython将dict列表作为新键附加到另一个dict
【发布时间】:2016-10-06 23:35:20
【问题描述】:

我有两个列表。第一个(列表 a)包含 dicts 列表,每个列表代表来自特定帖子的 cmets。它们都具有相同的“id”值。第二个列表(列表 b)仅包含 dicts,这些 dicts 是帖子。

现在我需要为 b_list 中的每个 dict 创建名为“cmets”的新键,并将 a_list 中的适当列表分配为值。因此,目标列表是 dict['id'] 与 post 值相同的列表。

a_list=[
        [{'id':'123', 'user':'Foo'}, {'id':'123','user':'Jonny'}, ...],
        [{'id':'456', 'user':'Bar'}, {'id':'456','user':'Mary'}, ...],
        ... 
       ]
b_list=[{'post':'123','text': 'Something'}, {'post':'456', 'text': 'Another thing'}, ...]

什么是最好的和更 Pythonic 的方式来做到这一点?

【问题讨论】:

  • 你的意思是b_list中的第一项应该转换成{'post':'123','text': 'Something', 'comments':[{'id':'123', 'user':'Foo'}, {'id':'123','user':'Jonny'}, ...]}
  • 正是@TigerhawkT3
  • a_listb_list 是否总是按照相应的顺序排列,第一个帖子 ID 是 '123',然后是 '456',等等?
  • 不,顺序可以不同

标签: python django list python-3.x dictionary


【解决方案1】:

构建一个 ID 字典,然后遍历它们:

>>> a_list=[
...         [{'id':'123', 'user':'Foo'}, {'id':'123','user':'Jonny'}, ],
...         [{'id':'456', 'user':'Bar'}, {'id':'456','user':'Mary'},],
...        ]
>>> b_list=[{'post':'123','text': 'Something'}, {'post':'456', 'text':'Another thing'}, ]
>>> d = {l[0]['id']:l for l in a_list}
>>> for item in b_list:
...     item['comments'] = d[item['post']]
...
>>> import pprint
>>> pprint.pprint(b_list)
[{'comments': [{'id': '123', 'user': 'Foo'}, {'id': '123', 'user': 'Jonny'}],
  'post': '123',
  'text': 'Something'},
 {'comments': [{'id': '456', 'user': 'Bar'}, {'id': '456', 'user': 'Mary'}],
  'post': '456',
  'text': 'Another thing'}]

【讨论】:

    【解决方案2】:

    我假设在a_list 中,一个嵌套的list 将具有相同的'id',并且每个id 只有一个列表。

    为了实现这一点,遍历 b_list 并检查 a_list 中的匹配。如果匹配,则为a_list的dict对象添加值

    >>> a_list=[
    ...         [{'id':'123', 'user':'Foo'}, {'id':'123','user':'Jonny'}],
    ...         [{'id':'456', 'user':'Bar'}, {'id':'456','user':'Mary'}],
    ...        ]
    >>> b_list=[{'post':'123','text': 'Something'}, {'post':'456', 'text': 'Another thing'}]
    >>>
    >>> for dict_item in b_list:
    ...     id = dict_item['post']
    ...     for list_item in a_list:
    ...         if list_item[0]['id'] == id:
    ...            dict_item['comments'] = list_item
    ...            break
    ...
    >>> b_list
    [{
         'text': 'Something', 
         'post': '123', 
         'comments': [
             {
                 'id': '123', 
                 'user': 'Foo'
             }, 
             {
                 'id': '123', 
                 'user': 'Jonny'
             }
          ]
      }, 
      {
          'post': '456', 
          'text': 'Another thing', 
          'comments': [
             {
                 'id': '456', 
                 'user': 'Bar'
             }, 
             {
                 'id': '456', 
                 'user': 'Mary'
             }
          ]
       }
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-17
      • 2020-02-25
      • 2021-06-07
      • 2014-09-21
      • 2020-12-16
      相关资源
      最近更新 更多