0

以下のデータ構造があります。

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

dict内のdictキーでソートされた順番のdictのこのリスト

4

1 に答える 1

4

バブルソートですか?本当に?

とにかく、独自のソート関数を書く必要はありません:

raw_sizes.sort(key=lambda d: d['selections']['size'])
于 2013-03-28T09:13:43.387 に答える