打印带有条件列表的词典

2024-06-16 11:07:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一张里面有字典的清单。如果网络追随者是>;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']}]

Tags: namenonetruelocationemptyfirstfollowinglast
3条回答
duck_collection = [{'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(ducks)
print(empty_list)

看起来你是在附加名字而不是字典。尝试用empty_list.append(ducks)替换empty_list.append(names)

列出你的情况

# d = [{'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']}]
[data for data in d if (data['followers'] - data['following'] >0)]

[{'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']}]

相关问题 更多 >