【问题标题】:Extract out nested keys into a list [duplicate]将嵌套键提取到列表中[重复]
【发布时间】:2018-06-10 21:31:11
【问题描述】:

我有一个字典 raw_data['list'],其值的结构如下:

k, v in sorted(raw_data['list'].items()):
    print(k, v)
    break

1001473688 {'resolved_id': '1001473688', 'item_id': '1001473688', 'word_count': '149', 'excerpt': '“The fraudulence paradox was that the more time and effort you put into trying to appear impressive or attractive to other people, the less impressive or attractive you felt inside — you were a fraud.', 'time_favorited': '0', 'favorite': '0', 'given_url': 'http://www.goodreads.com/quotes/564841-the-fraudulence-paradox-was-that-the-more-time-and-effort', 'is_index': '0', 'status': '0', 'sort_id': 3795, 'authors': {'3445796': {'author_id': '3445796', 'item_id': '1001473688', 'name': 'David Foster Wallace', 'url': 'http://www.goodreads.com/author/show/4339.David_Foster_Wallace'}}, 'time_read': '0', 'has_image': '0', 'has_video': '0', 'given_title': 'Quote by David Foster Wallace: “The fraudulence paradox was that the more t', 'resolved_title': '“The fraudulence paradox was that the more time and effort you put into trying to appear impressive or attractive to other people, the less impressive or attractive you felt inside — you were a fraud. And the more of a fraud you felt like, the harder you tried to convey an impressive or likable image of yourself so that other people wouldn’t find out what a hollow, fraudulent person you really were. Logically, you would think that the moment a supposedly intelligent nineteen-year-old became aware of this paradox, he’d stop being a fraud and just settle for being himself (whatever that was) because he’d figured out that being a fraud was a vicious infinite regress that ultimately resulted in being frightened, lonely, alienated, etc. But here was the other, higher-order paradox, which didn’t even have a form or name — I didn’t, I couldn’t.”', 'resolved_url': 'http://www.goodreads.com/quotes/564841-the-fraudulence-paradox-was-that-the-more-time-and-effort', 'time_added': '1438693251', 'time_updated': '1439849583', 'is_article': '1'}

raw_data['list'] 字典中的一些值有一个 'tags' 键,如下所示:

{'excerpt': '',
 'favorite': '0',
 'given_title': 'carlcheo.com/wp-content/uploads/2014/12/which-programming-language-should-i',
 'given_url': 'http://carlcheo.com/wp-content/uploads/2014/12/which-programming-language-should-i-learn-first-pdf.pdf',
 'has_image': '0',
 'has_video': '0',
 'is_article': '0',
 'is_index': '0',
 'item_id': '999554490',
 'resolved_id': '999554490',
 'resolved_title': '',
 'resolved_url': 'http://carlcheo.com/wp-content/uploads/2014/12/which-programming-language-should-i-learn-first-pdf.pdf',
 'sort_id': 3026,
 'status': '0',
 'tags': {'programming': {'item_id': '999554490', 'tag': 'programming'}},
 'time_added': '1454096378',
 'time_favorited': '0',
 'time_read': '0',
 'time_updated': '1454096385',
 'word_count': '0'}

我需要将“标签”键的所有键(也称为“标签”值的键)提取到一个列表中。我对嵌套字典没有太多经验,并且很难弄清楚我应该如何编写嵌套 for 循环(如果这是解决方案的最优雅方式)。请让我知道你的想法。谢谢!

【问题讨论】:

  • 1001473688 {'resolved_id': '1001473688',......} 无效。
  • 对不起,我进行了编辑以澄清..希望对您有所帮助
  • 您希望列表中的标签是唯一的吗?
  • 是的,但我更关心将它们全部提取到一个列表中

标签: python python-3.x dictionary for-loop nested


【解决方案1】:

下面的嵌套推导应该可以工作:

tags = [tag for v in raw_data['list'].values() for tag in v.get('tags', {})]

【讨论】:

  • 行得通。谢谢!!
猜你喜欢
  • 1970-01-01
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 2012-01-09
  • 2012-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多