遍历Python字典列表,并将每个字典放入新变量

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

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

提取定义的代码

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'}]}]}],'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'}]}]}],'lexicalCategory': {'id': 'verb','text': 'Verb'},'text': 'school'}],'type': 'headword','word': 'school'},{'id': 'school','lexicalEntries': [{'entries': [{'homographNumber': '200','senses': [{'deFinitions': ['a large group of fish or sea mammals'],'id': 'm_en_gbus0907280.005'}]}],{'entries': [{'homographNumber': '201','senses': 
[{'deFinitions': ['(of fish or sea mammals) form a large group'],'id': 'm_en_gbus0907280.009'}]}],'word': 'school'}],'word': 
'school'}

我要保持的联​​想是语音的一部分(例如名词或动词)。我想按上面示例输出中所示打印它。

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

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

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...