2010-10-20 41 views
8

我有建於以下方式一個奇怪的清單複雜列表:蟒蛇:如何排序在兩個不同的密鑰

[[name_d, 5], [name_e, 10], [name_a, 5]] 

,我想通過數(遞減),如果先對它進行排序的話,數字是相同的,按名稱(asc)。所以我想有結果是:

[[name_e, 10], [name_a, 5], [name_d, 5]] 

我試着認爲我可以在排序方法使用lambda函數,但我不知道我能做到這一點。

+1

[嵌套元組列表的高級排序標準]的可能重複(http://stackoverflow.com/questions/3831449/advanced-sorting-criteria-for-a-list-of-nested-tuples) – SilentGhost 2010-10-20 16:08:51

回答

20

排序功能允許通過一個函數作爲排序關鍵字:

l = [[name_d, 5], [name_e, 10], [name_a, 5]] 
# copy 
l_sorted = sorted(l, key=lambda x: (x[1] * -1, x[0])) 
# in place 
l.sort(key=lambda x: (x[1] * -1, x[0]) 

編輯: 1.排序順序2.證明副本和到位排序

+0

他需要降序排序的號碼 – SilentGhost 2010-10-20 16:07:00

+0

這不符合我想要的方式,因爲它也排序在desc(或asc)模式下的名稱 – 2010-10-20 16:09:20

+2

@Giovanni:它不是一個黑匣子。你看到它是如何解決的?你不能修改它以適應你的問題? – SilentGhost 2010-10-20 16:10:26

-2

它不需要是傳遞給sort方法的lambda函數,因爲它們是python中的第一類對象,所以實際上可以提供一個真正的函數。

L.sort(my_comparison_function) 

應該只是罰款在Python

+2

- 1比較函數在Python 3中消失,而另一個答案是未來證明。 – 2010-10-20 16:12:24

+0

我想過這個,但我怎麼能寫一個比較函數在兩個不同的鍵上工作?基本上我需要一個f((x [0],x [1]),(y [0],y [1])) – 2010-10-20 16:14:05

+0

@Steven:你在說什麼?這個答案可能沒用,但不是你說的原因。 [閱讀文檔](http://docs.python.org/py3k/library/stdtypes.html#mutable-sequence-types) – SilentGhost 2010-10-20 16:14:54

0

       這裏有一些我whipp編輯(解決相同類型的問題)。我只用我安裝的最新版本的Python(OS X)進行了檢查。低於進口部分是(clunkily命名)排序關鍵字:sortKeyWithTwoListOrderssortKeyWith2ndThen1stListValue


#Tested under Python 2.7.1 & Python 3.2.3: 

import random # Just to shuffle for demo purposes 

# Our two lists to sort 
firstCol=['abc','ghi','jkl','mno','bcd','hjk'] 
secondCol=[5,4,2,1] 

# Build 2 dimensional list [[firstCol,secondCol]...] 
myList = [] 
for firstInd in range(0, len(firstCol)): 
    for secondInd in range(0, len(secondCol)): 
    myList = myList + [[firstCol[firstInd],secondCol[secondInd]]] 

random.shuffle(myList) 

print ("myList (shuffled):") 
for i in range(0,len(myList)): 
    print (myList[i]) 

def sortKeyWithTwoListOrders(item): 
    return secondCol.index(item[1]), firstCol.index(item[0]) 

myList.sort(key=sortKeyWithTwoListOrders) 
print ("myList (sorted according to strict list order, second column then first column):") 
for i in range(0,len(myList)): 
    print (myList[i]) 

random.shuffle(myList) 

print ("myList (shuffled again):") 
for i in range(0,len(myList)): 
    print (myList[i]) 

def sortKeyWith2ndThen1stListValue(item): 
    return item[1], item[0] 

myList.sort(key=sortKeyWith2ndThen1stListValue) 
print ("myList (sorted according to *values*, second column then first column):") 
for i in range(0,len(myList)): 
    print (myList[i]) 

myList (shuffled): 
['ghi', 5] 
['abc', 2] 
['abc', 1] 
['abc', 4] 
['hjk', 5] 
['bcd', 4] 
['jkl', 5] 
['jkl', 2] 
['bcd', 1] 
['ghi', 1] 
['mno', 5] 
['ghi', 2] 
['hjk', 2] 
['jkl', 4] 
['mno', 4] 
['bcd', 2] 
['bcd', 5] 
['ghi', 4] 
['hjk', 4] 
['mno', 2] 
['abc', 5] 
['mno', 1] 
['hjk', 1] 
['jkl', 1] 
myList (sorted according to strict list order, second column then first column): 
['abc', 5] 
['ghi', 5] 
['jkl', 5] 
['mno', 5] 
['bcd', 5] 
['hjk', 5] 
['abc', 4] 
['ghi', 4] 
['jkl', 4] 
['mno', 4] 
['bcd', 4] 
['hjk', 4] 
['abc', 2] 
['ghi', 2] 
['jkl', 2] 
['mno', 2] 
['bcd', 2] 
['hjk', 2] 
['abc', 1] 
['ghi', 1] 
['jkl', 1] 
['mno', 1] 
['bcd', 1] 
['hjk', 1] 
myList (shuffled again): 
['hjk', 4] 
['ghi', 1] 
['abc', 5] 
['bcd', 5] 
['ghi', 4] 
['mno', 1] 
['jkl', 1] 
['abc', 1] 
['hjk', 1] 
['jkl', 2] 
['hjk', 5] 
['mno', 2] 
['jkl', 4] 
['ghi', 5] 
['bcd', 1] 
['bcd', 2] 
['jkl', 5] 
['abc', 2] 
['hjk', 2] 
['abc', 4] 
['mno', 4] 
['mno', 5] 
['bcd', 4] 
['ghi', 2] 
myList (sorted according to *values*, second column then first column): 
['abc', 1] 
['bcd', 1] 
['ghi', 1] 
['hjk', 1] 
['jkl', 1] 
['mno', 1] 
['abc', 2] 
['bcd', 2] 
['ghi', 2] 
['hjk', 2] 
['jkl', 2] 
['mno', 2] 
['abc', 4] 
['bcd', 4] 
['ghi', 4] 
['hjk', 4] 
['jkl', 4] 
['mno', 4] 
['abc', 5] 
['bcd', 5] 
['ghi', 5] 
['hjk', 5] 
['jkl', 5] 
['mno', 5] 
0

您可以兩次排序列表以獲得結果,只是反轉訂單:

import operator 

l = [[name_d, 5], [name_e, 10], [name_a, 5]] 

l.sort(operator.itemgetter(1)) 
l.sort(operator.itemgetter(0), reverse=True) 

然後您將按預期得到排序列表。