嗨,我有這個字典串列
[{'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]
uj5u.com熱心網友回復:
所以我從您的問題中了解到,您需要存盤每個用戶字典的followers和following值的差異,并將其存盤在一個名為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清單。
uj5u.com熱心網友回復:
您必須遍歷串列中的所有專案并計算每個專案的趨勢,然后將其附加到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]
uj5u.com熱心網友回復:
您可以使用串列理解。
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)
uj5u.com熱心網友回復:
不是最易讀的答案,但功能方法是:
>>> from operator import sub, itemgetter
>>> from itertools import starmap
>>> list(starmap(sub, map(itemgetter('followers', 'following'), duck_collection)))
[12745, -4877, 40188]
你可以使用 justoperator.itemgetter和 a comprehension:
[x - y for x, y in map(itemgetter('followers', 'following'), duck_collection)]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/314386.html
