我有一個里面有字典的串列。如果凈關注者 > 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': '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']}]
到目前為止,我已經在下面得到了這個代碼。但它只列印字典的 firs_names。我希望將兩個字典都附加到空串列中。有什么建議嗎?
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']}]
uj5u.com熱心網友回復:
根據您的情況列出理解
# 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']}]
uj5u.com熱心網友回復:
看起來您附加的是名字而不是字典。嘗試替換empty_list.append(names)為empty_list.append(ducks)
uj5u.com熱心網友回復:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/311464.html
上一篇:從串列中回傳最大序列
下一篇:如何獲取R中多個連續元素的索引?
