0
我有以下數據結構:排序列表字典由字典裏面字典的關鍵在Python
[
{
original_price: {
value: ""
},
promos: [ ],
local_id: "122863",
url: "link",
title: "abc",
current_price: {
value: 19.5
},
image: "link",
selections: {
color: "BLACK",
length: "",
size: "7"
},
store: "abc"
},
{
original_price: {
value: ""
},
promos: [ ],
local_id: "122863",
url: "link",
title: "abc",
current_price: {
value: 19.5
},
image: "link",
selections: {
color: "BLACK",
length: "",
size: "9"
},
store: "abc"
},
{
original_price: {
value: ""
},
promos: [ ],
local_id: "122863",
url: "link",
title: "abc",
current_price: {
value: 19.5
},
image: "link",
selections: {
color: "BLACK",
length: "",
size: "6"
},
store: "abc"
}
]
我試着用代碼「大小」鍵對它進行排序:
def order_size(self, raw_sizes):
for i in range(len(raw_sizes)):
print i
for j in range(i+1, len(raw_sizes)):
#print raw_sizes
if raw_sizes[j]['selections']['size'] < raw_sizes[i]['selections']['size']:
raw_sizes[j], raw_sizes[i] = raw_sizes[i], raw_sizes[j]
#print raw_sizes
return raw_sizes
但我不能。請幫幫我。 我期望的結果是:
[
{
original_price: {
value: ""
},
promos: [ ],
local_id: "122863",
url: "link",
title: "abc",
current_price: {
value: 19.5
},
image: "link",
selections: {
color: "BLACK",
length: "",
size: "6"
},
store: "abc"
},
{
original_price: {
value: ""
},
promos: [ ],
local_id: "122863",
url: "link",
title: "abc",
current_price: {
value: 19.5
},
image: "link",
selections: {
color: "BLACK",
length: "",
size: "7"
},
store: "abc"
},
{
original_price: {
value: ""
},
promos: [ ],
local_id: "122863",
url: "link",
title: "abc",
current_price: {
value: 19.5
},
image: "link",
selections: {
color: "BLACK",
length: "",
size: "9"
},
store: "abc"
}
]
這是爲了字典的列表進行了排序的字典裏面的關鍵字典
我覺得關鍵FUNC應該是拉姆達d:d [ '選擇'] [ '大小'] – 2013-03-28 09:17:33
@PiotrHajduga感謝,改變 - 我無法在解釋器中檢查它,因爲原始的「dict」不是Python語法。 – 2013-03-28 09:18:47
是的,這是泡泡。我嘗試你的建議,但異常類型:\t KeyError異常 異常值:\t 「大小」。帶排序功能我無法訪問字典裏面的字典鍵 – 2013-03-28 09:19:10