如何遍历字典列表以获取特定值?

时间:2019-09-10 05:20:41

标签: python-3.x list dictionary

我如何从下面的词典列表中获取所有标题。标题字段存储在“数据”键中,在某些情况下,数据键可以为空列表。

all_movies = [{'page': '1',
               'per_page': 10,
               'total': 13,
               'total_pages': 2,
               'data': [{'Title': 'Italian Spiderman', 'Year': 2007, 'imdbID': 'tt2705436'},
                        {'Title': 'Superman, Spiderman or Batman',
                         'Year': 2011,
                         'imdbID': 'tt2084949'},
                        {'Title': 'Spiderman', 'Year': 1990, 'imdbID': 'tt0100669'},
                        {'Title': 'Spiderman', 'Year': 2010, 'imdbID': 'tt1785572'},
                        {'Title': 'Fighting, Flying and Driving: The Stunts of Spiderman 3',
                         'Year': 2007,
                         'imdbID': 'tt1132238'},
                        {'Title': 'Spiderman and Grandma', 'Year': 2009, 'imdbID': 'tt1433184'},
                        {'Title': 'The Amazing Spiderman T4 Premiere Special',
                         'Year': 2012,
                         'imdbID': 'tt2233044'},
                        {'Title': 'Amazing Spiderman Syndrome',
                         'Year': 2012,
                         'imdbID': 'tt2586634'},
                        {'Title': "Hollywood's Master Storytellers: Spiderman Live",
                         'Year': 2006,
                         'imdbID': 'tt2158533'},
                        {'Title': 'Spiderman 5', 'Year': 2008, 'imdbID': 'tt3696826'}]},
              {'page': '2',
               'per_page': 10,
               'total': 13,
               'total_pages': 2,
               'data': [{'Title': 'They Call Me Spiderman',
                         'Year': 2016,
                         'imdbID': 'tt5861236'},
                        {'Title': 'The Death of Spiderman', 'Year': 2015, 'imdbID': 'tt5921428'},
                        {'Title': 'Spiderman in Cannes', 'Year': 2016, 'imdbID': 'tt5978586'}]},
              {'page': '3', 'per_page': 10, 'total': 13, 'total_pages': 2, 'data': []}]

这是我需要的输出:

title = ['Amazing Spiderman Syndrome',
'Fighting, Flying and Driving: The Stunts of Spiderman 3',
"Hollywood's Master Storytellers: Spiderman Live",
'Italian Spiderman',
'Spiderman',
'Spiderman',
'Spiderman 5',
'Spiderman and Grandma',
'Superman, Spiderman or Batman',
'The Amazing Spiderman T4 Premiere Special']
all_title = []
for movie in all_movies:
    title = movie['data']
    all_title.append(title)

这给了我下面的字典列表,但是我无法使用列表索引获取标题

2 个答案:

答案 0 :(得分:2)

我想像这样

  titles = []
  for movie in all_movies:
    title_list = movie.get("data", [])
    for title in title_list:
      if title.get('Title'):
        titles.append(title.get('Title'))

  print(titles)

如果缺少密钥,请考虑将get与默认参数一起使用

答案 1 :(得分:1)

@ vx3r的答案比较简单,但是出于完整性考虑,使用理解也是一样:

import itertools
import json

all_movies = [{'page': '1',
               'per_page': 10,
               'total': 13,
               'total_pages': 2,
               'data': [{'Title': 'Italian Spiderman', 'Year': 2007, 'imdbID': 'tt2705436'},
                        {'Title': 'Superman, Spiderman or Batman',
                         'Year': 2011,
                         'imdbID': 'tt2084949'},
                        {'Title': 'Spiderman', 'Year': 1990, 'imdbID': 'tt0100669'},
                        {'Title': 'Spiderman', 'Year': 2010, 'imdbID': 'tt1785572'},
                        {'Title': 'Fighting, Flying and Driving: The Stunts of Spiderman 3',
                         'Year': 2007,
                         'imdbID': 'tt1132238'},
                        {'Title': 'Spiderman and Grandma', 'Year': 2009, 'imdbID': 'tt1433184'},
                        {'Title': 'The Amazing Spiderman T4 Premiere Special',
                         'Year': 2012,
                         'imdbID': 'tt2233044'},
                        {'Title': 'Amazing Spiderman Syndrome',
                         'Year': 2012,
                         'imdbID': 'tt2586634'},
                        {'Title': "Hollywood's Master Storytellers: Spiderman Live",
                         'Year': 2006,
                         'imdbID': 'tt2158533'},
                        {'Title': 'Spiderman 5', 'Year': 2008, 'imdbID': 'tt3696826'}]},
              {'page': '2',
               'per_page': 10,
               'total': 13,
               'total_pages': 2,
               'data': [{'Title': 'They Call Me Spiderman',
                         'Year': 2016,
                         'imdbID': 'tt5861236'},
                        {'Title': 'The Death of Spiderman', 'Year': 2015, 'imdbID': 'tt5921428'},
                        {'Title': 'Spiderman in Cannes', 'Year': 2016, 'imdbID': 'tt5978586'}]},
              {'page': '3', 'per_page': 10, 'total': 13, 'total_pages': 2, 'data': []}]

# get all 'data' contents together, then flattened
datas = list(itertools.chain.from_iterable([l.get('data', []) for l in all_movies]))
# extract only titles, all of them
all_titles = [k.get('Title') for k in datas]
print(json.dumps(all_titles, indent=2))

输出:

[
  "Italian Spiderman",
  "Superman, Spiderman or Batman",
  "Spiderman",
  "Spiderman",
  "Fighting, Flying and Driving: The Stunts of Spiderman 3",
  "Spiderman and Grandma",
  "The Amazing Spiderman T4 Premiere Special",
  "Amazing Spiderman Syndrome",
  "Hollywood's Master Storytellers: Spiderman Live",
  "Spiderman 5",
  "They Call Me Spiderman",
  "The Death of Spiderman",
  "Spiderman in Cannes"
]

您有一些重复的条目。如果这不是您想要的,请使用集合而不是列表:

all_titles = {k.get('Title') for k in datas}