【发布时间】:2020-04-26 20:46:42
【问题描述】:
所以,我有一个嵌套列表,我需要使用 def 按值排序。问题是,该函数使用键对列表进行排序,因为没有正确调用另一个函数。 它不断重复。
currencies=[['hryvna', '1 hryvna', '2005', '0.036'], ['dollar', '1 dollar', '2006', '1'], ['euro', '1 euro', '2007', '1.08'], ['Belarusian ruble', '1 Belarusian ruble', '2008', '0.39'], ['pound', '1 pound', '2009', '1.23']]
def atribute_choice():
atribute=int(input("""Choose the atribute:
1 - Currency name;
2 - Currency denomination;
3 - Currency year of release;
4 - Currency rate to the dollar\n"""))
atribute=atribute-1
return atribute
def sorting(elem):
return elem[atribute_choice()]
def currencies_sorting():
currencies.sort(key=sorting)
currencies_sorting()
print (currencies)
【问题讨论】:
标签: python list function sorting