繁体   English   中英

如何使用嵌套字典从列表中打印出特定内容

[英]How to print out specific content from list with nested dictionaries

嗨,我有这个字典列表

[{'first_name': 'Davey', 'last_name': 'McDuck', 'location': "Rob's Office", 'insane': True, 'followers': 12865, 'following': 120, 'weapons': ['wit', 'steely stare', 'devilish good looks'], 'remorse': None}, {'first_name': 'Jim', 'last_name': 'Bob', 'location': 'Turing Lab', 'insane': False, 'followers': 123, 'following': 5000, 'weapons': ['squeak'], 'remorse': None}, {'first_name': 'Celest', 'last_name': '', 'location': 'Throne Room', 'insane': True, 'followers': 40189, 'following': 1, 'weapons': ['politics', 'dance moves', 'chess grandmaster', 'immortality']}]

我在这个列表中有三本词典,我需要打印每本词典的推特趋势,即推特关注者 - 关注 = 趋势。 此外,我需要创建一个空列表并将这 3 个值附加到该空列表中。 到目前为止,我在任何建议下都得到了这个代码吗?

trend = []
followers = int(duck_collection['followers']) - int(duck_collection['following'])
for f in followers:
    print(f)
    trend.append(f)
print(trend)

输出应该是:

12745
-4877
40188
Trend: [12745, -4877, 40188]

所以我从您的问题中了解到,您需要存储每个用户字典的followersfollowing值的差异,并将其存储在一个名为trend的空列表中。

您可以使用列表理解在几行中完成,如下所示:

user_stats = [{'first_name': 'Davey', 'last_name': 'McDuck', 'location': "Rob's Office", 'insane': True, 'followers': 12865, 'following': 120, 'weapons': ['wit', 'steely stare', 'devilish good looks'], 'remorse': None}, {'first_name': 'Jim', 'last_name': 'Bob', 'location': 'Turing Lab', 'insane': False, 'followers': 123, 'following': 5000, 'weapons': ['squeak'], 'remorse': None}, {'first_name': 'Celest', 'last_name': '', 'location': 'Throne Room', 'insane': True, 'followers': 40189, 'following': 1, 'weapons': ['politics', 'dance moves', 'chess grandmaster', 'immortality']}]

trend = [user["followers"] - user["following"] for user in user_stats]

因此,您将获得trend列表。

您必须遍历列表中的所有项目并计算每个项目的趋势,然后将其附加到trend列表中。

trend = []
for item in duck_collection:
    current_trend = item['followers']-item['following']
    print(current_trend)
    trend.append(current_trend)
print(trend)

或者,无需打印,您可以使用列表理解来创建trend列表 -

trend = [item['followers']-item['following'] for item in duck_collection]

您可以使用列表理解。

l = [{'first_name': 'Davey', 'last_name': 'McDuck', 'location': "Rob's Office", 'insane': True, 'followers': 12865, 'following': 120, 'weapons': ['wit', 'steely stare', 'devilish good looks'], 'remorse': None}, {'first_name': 'Jim', 'last_name': 'Bob', 'location': 'Turing Lab', 'insane': False, 'followers': 123, 'following': 5000, 'weapons': ['squeak'], 'remorse': None}, {'first_name': 'Celest', 'last_name': '', 'location': 'Throne Room', 'insane': True, 'followers': 40189, 'following': 1, 'weapons': ['politics', 'dance moves', 'chess grandmaster', 'immortality']}]
trend = [x['followers'] - x['following'] for x in l]
print(trend)

不是最易读的答案,但功能方法是:

>>> from operator import sub, itemgetter
>>> from itertools import starmap
>>> list(starmap(sub, map(itemgetter('followers', 'following'), duck_collection)))
[12745, -4877, 40188]

你可以只使用operator.itemgetter和理解:

[x - y for x, y in map(itemgetter('followers', 'following'), duck_collection)]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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