我對python編程非常陌生,至今尚未購買關於此問題的教科書(我今天從商店或Amazon購買)。同時,你能幫我解決我遇到的以下問題嗎?字典對象列表的數字排序
我有這樣的字典對象的列表:
stock = [
{ 'date': '2012', 'amount': '1.45', 'type': 'one'},
{ 'date': '2012', 'amount': '1.4', 'type': 'two'},
{ 'date': '2011', 'amount': '1.35', 'type': 'three'},
{ 'date': '2012', 'amount': '1.35', 'type': 'four'}
]
我想通過量日期列由量列對列表進行排序,然後使排序列表如下:
stock = [
{ 'date': '2011', 'amount': '1.35', 'type': 'three'},
{ 'date': '2012', 'amount': '1.35', 'type': 'four'},
{ 'date': '2012', 'amount': '1.4', 'type': 'two'},
{ 'date': '2012', 'amount': '1.45', 'type': 'one'}
]
我現在覺得我需要使用sorted(),但作爲初學者,我很難理解我所看到的概念。
我嘗試這樣做:
from operator import itemgetter
all_amounts = itemgetter("amount")
stock.sort(key = all_amounts)
,但是這導致在了排序的字母數字,而不是數字的名單。
有人可以告訴我如何實現這個看似簡單的排序嗎?謝謝!
你爲什麼不把你的字典中的數據轉換成數字?這似乎是一個更好的代表。 – BrenBarn
你的'股票'是無效的Python。請修復它 –