简体   繁体   中英

How to get the values from a dictionary nested within two lists?

I'm still familiarizing with python and using APIs. I extracted the following data from an API:

    [[{'term': 'Boys', 'AAT_URL': 'http://vocab.getty.edu/page/aat/300247598'},
  {'term': 'Fish', 'AAT_URL': 'http://vocab.getty.edu/page/aat/300266085'},
  {'term': 'Boats', 'AAT_URL': 'http://vocab.getty.edu/page/aat/300178749'}],
 [{'term': 'Bears', 'AAT_URL': 'http://vocab.getty.edu/page/aat/300266516'}],
 None,
  [{'term': 'Interiors',
   'AAT_URL': 'http://vocab.getty.edu/page/aat/300391239'},
  {'term': 'Jewelry', 'AAT_URL': 'http://vocab.getty.edu/page/aat/300209286'},
  {'term': 'Couples', 'AAT_URL': 'http://vocab.getty.edu/page/aat/300379217'},
  {'term': 'Men', 'AAT_URL': 'http://vocab.getty.edu/page/aat/300025928'},
  {'term': 'Women', 'AAT_URL': 'http://vocab.getty.edu/page/aat/300025943'},
  {'term': 'Weights and Measures',
   'AAT_URL': 'http://vocab.getty.edu/page/aat/300386648'}],
...]

I would like to extract the values within the 'term' keys (but preserving the sublists) in a final list like this:

[['Boys','Fish','Boats'],['Bears'],['Interiors','Jewlery','Couples','Men','Women','Weights and Measures']...]

Whenever I try to Iter through the lists like this:

for x in tags:
    for y in x:
        print (y['term'])

I get the following result:

Boys
Fish
Boats
Bears

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-48-f2ff632ad612> in <module>
     11 
     12 for x in tags:
---> 13     for y in x:
     14         print (y['term'])

TypeError: 'NoneType' object is not iterable

How can I solve it? Thank you in advance c:

Check x against None before iterating through it:

for x in tags:
    if x is not None:
        for y in x:
            print (y['term'])

If you see there is also an element of None in the data you have extracted from API. So you will have to check you do not encounter that while iterating.

for x in tags:
    if(x is not None):
        for y in x:
            print(y['term'])

This will take care of the problem

To extract the data into a list of same structure check for None and only add things if you have data:

data = [[{'term': 'Boys', 'AAT_URL': 'http://vocab.getty.edu/page/aat/300247598'},
         {'term': 'Fish', 'AAT_URL': 'http://vocab.getty.edu/page/aat/300266085'},
         {'term': 'Boats', 'AAT_URL': 'http://vocab.getty.edu/page/aat/300178749'}],
        [{'term': 'Bears', 'AAT_URL': 'http://vocab.getty.edu/page/aat/300266516'}], 
        None,
        [{'term': 'Interiors', 'AAT_URL': 'http://vocab.getty.edu/page/aat/300391239'},
         {'term': 'Jewelry', 'AAT_URL': 'http://vocab.getty.edu/page/aat/300209286'},
         {'term': 'Couples', 'AAT_URL': 'http://vocab.getty.edu/page/aat/300379217'},
         {'term': 'Men', 'AAT_URL': 'http://vocab.getty.edu/page/aat/300025928'},
         {'term': 'Women', 'AAT_URL': 'http://vocab.getty.edu/page/aat/300025943'},
         {'term': 'Weights and Measures', 'AAT_URL': 'http://vocab.getty.edu/page/aat/300386648'}]]

result = []
for inner_list in data:
    if inner_list:  # None and [] are falsy, ignore them

        # got something, add a new empty list to add things into
        result.append([])
        for d in inner_list:
            # add all dicts term to the last item of result
            result[-1].append(d["term"])  

print(result)

will output:

[['Boys', 'Fish', 'Boats'], ['Bears'], 
 ['Interiors', 'Jewelry', 'Couples', 'Men', 'Women', 'Weights and Measures']]

first filter out None and then use map function

    data = [
        [
            {"term": "Boys", "AAT_URL": "http://vocab.getty.edu/page/aat/300247598"},
            {"term": "Fish", "AAT_URL": "http://vocab.getty.edu/page/aat/300266085"},
            {"term": "Boats", "AAT_URL": "http://vocab.getty.edu/page/aat/300178749"},
        ],
        [{"term": "Bears", "AAT_URL": "http://vocab.getty.edu/page/aat/300266516"}],
        None,
        [
            {"term": "Interiors", "AAT_URL": "http://vocab.getty.edu/page/aat/300391239"},
            {"term": "Jewelry", "AAT_URL": "http://vocab.getty.edu/page/aat/300209286"},
            {"term": "Couples", "AAT_URL": "http://vocab.getty.edu/page/aat/300379217"},
            {"term": "Men", "AAT_URL": "http://vocab.getty.edu/page/aat/300025928"},
            {"term": "Women", "AAT_URL": "http://vocab.getty.edu/page/aat/300025943"},
            {"term": "Weights and Measures", "AAT_URL": "http://vocab.getty.edu/page/aat/300386648"},
        ],
    ]


    result = map(lambda x: list(map(lambda i: i["term"], x)), filter(lambda x: x is not None, data))
    print(list(result))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM