我有一个以下列方式构建的奇怪列表:
[[name_d, 5], [name_e, 10], [name_a, 5]]
我希望先用数字(desc)对它进行排序,然后,如果数字相同,则按名称(asc)排序。所以我希望得到的结果是:
[[name_e, 10], [name_a, 5], [name_d, 5]]
我试着想一个我可以在sort方法中使用的lambda函数,但我不确定我能做到。
答案 0 :(得分:24)
python中的排序函数允许将函数作为排序键传递:
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.展示复制和就地排序
答案 1 :(得分:0)
这是我掀起的事情(解决相同类型的问题)。我只用最新版本的已安装Python(OS X)检查过它。下面的导入部分是(clunkily-named)排序键: sortKeyWithTwoListOrders 和 sortKeyWith2ndThen1stListValue
#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]
答案 2 :(得分: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)
然后您将按预期获得排序列表。
答案 3 :(得分:-2)
它不需要是传递给sort
方法的lambda函数,实际上你可以提供一个真正的函数,因为它们是python中的第一类对象。
L.sort(my_comparison_function)
应该可以正常工作