【问题标题】:Iterate through a list of Python dictionaries and place each dictionary into a new variable遍历 Python 字典列表并将每个字典放入一个新变量中
【发布时间】:2020-08-15 16:55:31
【问题描述】:

我有从 API 返回的嵌套列表和字典。每个 ["lexicalEntries"][0] 字典中的最后三个键是相关“条目”中所有定义的图例。

dd = {
    "metadata": {...},
    "results": [
        {
            "lexicalEntries": [
                {
                    "entries": [
                        ...definitions
                    ],
                    "language": "en-us",
                    "lexicalCategory": {"id": "noun", "text": "Noun"},
                    "text": "school",
                },
                {
                    "entries": [
                        ...more definitions
                    ],
                    "language": "en-us",
                    "lexicalCategory": {"id": "verb", "text": "Verb"},
                    "text": "school",
                },
            ],
        },
        {
            "lexicalEntries": [
                {
                    "entries": [
                        ...more definitions
                    ],
                    "language": "en-us",
                    "lexicalCategory": {"id": "noun", "text": "Noun"},
                    "text": "school",
                },
                {
                    "entries": [
                        ...more definitions
                    ],
                    "language": "en-us",
                    "lexicalCategory": {"id": "verb", "text": "Verb"},
                    "text": "school",
                },
            ],
        },
    ],
}

提取定义的代码:

def gen_dict_extract(key, var):
    if hasattr(var, "items"):
        for k, v in var.items():
            if k == key:
                yield v
            if isinstance(v, dict):
                for result in gen_dict_extract(key, v):
                    yield result
            elif isinstance(v, list):
                for d in v:
                    for result in gen_dict_extract(key, d):
                        yield result


count = len(list(gen_dict_extract("definitions", dd)))
gendList = list(gen_dict_extract("definitions", dd))

print(f"\nResults: {count}\n")

x = 1
for i in gendList:
    print(f"{x}. {i[0].capitalize()}.\n")
    x += 1

它会打印:

Results: 13

1. An institution for educating children.

2. The buildings used by an institution for educating children.

3 - 11

12. A large group of fish or sea mammals.

13. (of fish or sea mammals) form a large group.

我希望它打印这个示例输出:

Results: 13

Noun

1. An institution for educating children.

2 - 9

Verb

10. Send to school; educate.

11 - 13

这是完整的 API 结果:

{'id': 'school', 'metadata': {'operation': 'retrieve', 'provider': 'Oxford University Press', 'schema': 'RetrieveEntry'}, 'results': [{'id': 'school', 'language': 'en-us', 'lexicalEntries': [{'entries': [{'homographNumber': '100', 'senses': [{'definitions': ['an institution for educating children'], 'id': 'm_en_gbus0907270.006', 'subsenses': [{'definitions': ['the buildings used by an institution 
for educating children'], 'id': 'm_en_gbus0907270.009'}, {'definitions': ['the students and staff of a school'], 'id': 'm_en_gbus0907270.010'}, {'definitions': 
["a day's work at school"], 'id': 'm_en_gbus0907270.012'}]}, {'definitions': ['any institution at which instruction is given in a particular discipline'], 'id': 'm_en_gbus0907270.016', 'subsenses': [{'definitions': ['a university'], 'id': 'm_en_gbus0907270.017'}, {'definitions': ['a department or faculty of a college concerned with a particular subject of study'], 'id': 'm_en_gbus0907270.018'}]}, 
{'definitions': ['a group of people, particularly writers, artists, or philosophers, sharing the same or similar ideas, methods, or style'], 'id': 'm_en_gbus0907270.020', 'subsenses': [{'definitions': ['a style, approach, or method of a specified character'], 'id': 'm_en_gbus0907270.021'}]}]}], 'language': 'en-us', 'lexicalCategory': {'id': 'noun', 'text': 'Noun'}, 'text': 'school'}, {'entries': [{'homographNumber': '101', 'senses': [{'definitions': ['send to school; educate'], 'id': 'm_en_gbus0907270.041', 'subsenses': [{'definitions': ['train or discipline (someone) in a particular skill or activity'], 'id': 'm_en_gbus0907270.047'}]}]}], 'language': 'en-us', 'lexicalCategory': {'id': 'verb', 'text': 'Verb'}, 
'text': 'school'}], 'type': 'headword', 'word': 'school'}, {'id': 'school', 'language': 'en-us', 'lexicalEntries': [{'entries': [{'homographNumber': '200', 'senses': [{'definitions': ['a large group of fish or sea mammals'], 'id': 'm_en_gbus0907280.005'}]}], 'language': 'en-us', 'lexicalCategory': {'id': 'noun', 'text': 'Noun'}, 'text': 'school'}, {'entries': [{'homographNumber': '201', 'senses': 
[{'definitions': ['(of fish or sea mammals) form a large group'], 'id': 'm_en_gbus0907280.009'}]}], 'language': 'en-us', 'lexicalCategory': {'id': 'verb', 'text': 'Verb'}, 'text': 'school'}], 'type': 'headword', 'word': 'school'}], 'word': 
'school'}

我想要保持的关联是词性(例如名词或动词)。我想在示例输出中如上所示打印它。

我不知道解决这个问题的最佳方法。请帮助一个新手。即使你可以指导我如何解决这个问题。

参考:我使用了这篇文章中的生成器: Find all occurrences of a key in nested dictionaries and lists

【问题讨论】:

  • 你已经从 API 中给出了完整的结果,这很棒,但很难通过口头描述来识别你想要做什么。(你能在每个部分中给出例子吗?)(在哪里您在 API 输出中引用的字典列表?)(API 输出是如何结构化的,其中有很多嵌套的字典和列表?)(您计划保留的输出数据结构是什么?关联'?也许给出预期的输出示例?)检查如何minimal reproducible example,以便SO上的其他人可以帮助你。
  • 感谢您的浏览。我可以尝试更清楚。请给我一点时间。
  • 我更新了,希望更清楚。

标签: python-3.x list dictionary nested data-science


【解决方案1】:

此代码应获取按类别(名词\动词)分组的主要定义。我跳过了子意义定义。

dd = {'id': 'school', 'metadata': {'operation': 'retrieve', 'provider': 'Oxford University Press', 'schema': 'RetrieveEntry'
    }, 'results': [
        {'id': 'school', 'language': 'en-us', 'lexicalEntries': [

      ...........
      
    ], 
    'word': 'school'
}


dd2 = {}

dd2[dd['word']] = []
for d in dd['results']: # assume all ids are same word 
   s = {}
   for le in d['lexicalEntries']:  # noun\verb
       key = le['lexicalCategory']['id']  # noun\verb
       df = []
       for ss in le['entries'][0]['senses']:  # each definition
          df.append(ss['definitions'][0])
       s[key] = df
   dd2[dd['word']].append(s)
   
print(dd2)
   

输出:

{'school': [
        {
         'noun': ['an institution for educating children', 
                  'any institution at which instruction is given in a particular discipline', 
                  'a group of people, particularly writers, artists, or philosophers, sharing the same or similar ideas, methods, or style'], 
         'verb': ['send to school; educate']
        },
        {
         'noun': ['a large group of fish or sea mammals'], 
         'verb': ['(of fish or sea mammals) form a large group']
        }
    ]
}

【讨论】:

  • 非常感谢您抽出宝贵时间。我现在正在使用您的代码以达到基本要求。我会告诉你我改变了什么来获得subsense。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-22
  • 2019-11-21
  • 1970-01-01
  • 1970-01-01
  • 2014-09-05
  • 2019-09-02
  • 1970-01-01
相关资源
最近更新 更多