我有一张里面有字典的清单。如果网络追随者是>;0(Followers-following)我想打印出那本词典
[{'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']}]
到目前为止,我已经得到了下面的代码。但它只是印刷字典的名字。我想把这两个单词都加到空名单上。有什么建议吗
empty_list = []
for ducks in duck_collection:
current_trend = ducks['followers']-ducks['following']
if current_trend > 0:
names = ducks['first_name']
empty_list.append(names)
print(empty_list)
电流输出:
['Davey', 'Celest']
期望输出:
[{'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': 'Celest', 'last_name': '', 'location': 'Throne Room', 'insane': True, 'followers': 40189, 'following': 1, 'weapons': ['politics', 'dance moves', 'chess grandmaster', 'immortality']}]
看起来你是在附加名字而不是字典。尝试用
empty_list.append(ducks)
替换empty_list.append(names)
列出你的情况
相关问题 更多 >
编程相关推荐