3 回答

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個(gè)贊
您可以將key參數(shù)用于list.sort():
my_list.sort(key=lambda x: x[1])
或者,更快一點(diǎn)
my_list.sort(key=operator.itemgetter(1))
(與任何模塊一樣,您將需要import operator能夠使用它。)

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超9個(gè)贊
如果您使用的是python 3.x,則可以sorted在mylist上應(yīng)用該函數(shù)。 這只是@Sven Marnach上面給出的答案的補(bǔ)充。
# using *sort method*
mylist.sort(lambda x: x[1])
# using *sorted function*
sorted(mylist, key = lambda x: x[1])

TA貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊
def findMaxSales(listoftuples):
newlist = []
tuple = ()
for item in listoftuples:
movie = item[0]
value = (item[1])
tuple = value, movie
newlist += [tuple]
newlist.sort()
highest = newlist[-1]
result = highest[1]
return result
movieList = [("Finding Dory", 486), ("Captain America: Civil
War", 408), ("Deadpool", 363), ("Zootopia", 341), ("Rogue One", 529), ("The Secret Life of Pets", 368), ("Batman v Superman", 330), ("Sing", 268), ("Suicide Squad", 325), ("The Jungle Book", 364)]
print(findMaxSales(movieList))
輸出->俠盜一號(hào)
添加回答
舉報(bào)