用元组排序字典

2024-06-26 17:44:48 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,为了好玩和练习,我一直跟随一位大学朋友和他们的高级编程课,并处理分配给他们的任务,以便为同一个Python课程做好准备。我现在在字典和元组中,根据指定听写中从最小到最大的数字对它们进行排序。到目前为止,我有以下代码:

cityRevenues = {'Alabaster':[40,50,23,18], 'Anniston':[56,78,34,11], 
               'Athens':[40,34,18,30],'Auburn':[55,67,23,11],
               'Decatur':[44,23,56,11],'Florence':[55,67,33,23],'Gadsden':[45,67,54,77]}

a = (sorted(cityRevenues.items(), key=lambda x: x[1]))
print('Sort the cities by their Quarter 1 Revenues.')
print("")
print(a)
print("")
b = (sorted(cityRevenues.items(), key=lambda x: x[1][1]))
print('Sort the cities by their Quarter 2 Revenues.')
print("")
print(b)
print("")
c = (sorted(cityRevenues.items(), key=lambda x: x[1][2]))
print("Sort the cities by their Quarter 3 Revenues.")
print("")
print(c)
print("")
d = (sorted(cityRevenues.items(), key=lambda x: x[1][3]))
print("Sort the cities by their Quarter 4 Revenues.")
print("")
print(d)
print("")

输出:

Sort the cities by their Quarter 1 Revenues.

[('Athens', [40, 34, 18, 30]), ('Alabaster', [40, 50, 23, 18]), ('Decatur', [44, 23, 56, 11]), ('Gadsden', [45, 67, 54, 77]), ('Auburn', [55, 67, 23, 11]), ('Florence', [55, 67, 33, 23]), ('Anniston', [56, 78, 34, 11])]

Sort the cities by their Quarter 2 Revenues.

[('Decatur', [44, 23, 56, 11]), ('Athens', [40, 34, 18, 30]), ('Alabaster', [40, 50, 23, 18]), ('Auburn', [55, 67, 23, 11]), ('Florence', [55, 67, 33, 23]), ('Gadsden', [45, 67, 54, 77]), ('Anniston', [56, 78, 34, 11])]

Sort the cities by their Quarter 3 Revenues.

[('Athens', [40, 34, 18, 30]), ('Auburn', [55, 67, 23, 11]), ('Alabaster', [40, 50, 23, 18]), ('Florence', [55, 67, 33, 23]), ('Anniston', [56, 78, 34, 11]), ('Gadsden', [45, 67, 54, 77]), ('Decatur', [44, 23, 56, 11])]

Sort the cities by their Quarter 4 Revenues.

[('Auburn', [55, 67, 23, 11]), ('Decatur', [44, 23, 56, 11]), ('Anniston', [56, 78, 34, 11]), ('Alabaster', [40, 50, 23, 18]), ('Florence', [55, 67, 33, 23]), ('Athens', [40, 34, 18, 30]), ('Gadsden', [45, 67, 54, 77])]

我设法根据每个元组中从最小到最大的值对它们进行排序,但我不知道如何将其编码到只显示元组中最大值的位置。i、 电子邮件:

Sort the cities by their Quarter 1 Revenues.

[('Athens', [40]), ('Alabaster', [40]), ('Decatur', [44]), ('Gadsden', [45]), ('Auburn', [55]), ('Florence', [55]), ('Anniston', [56])]

我怎样才能让特定的元组值成为唯一的打印值呢?你知道吗


Tags: thebysortprintcitiestheiralabasterquarter
3条回答

因为最大的元素在列表的末尾,所以可以使用索引-1

例如

cityRevenues = {'Alabaster':[40,50,23,18], 'Anniston':[56,78,34,11], 
               'Athens':[40,34,18,30],'Auburn':[55,67,23,11],
               'Decatur':[44,23,56,11],'Florence':[55,67,33,23],'Gadsden':[45,67,54,77]}

a = (sorted(cityRevenues.items(), key=lambda x: x[1]))
print('Sort the cities by their Quarter 1 Revenues.')
print("\n")
print(a[-1])
print("\n")

它会给你

('Anniston', [56, 78, 34, 11])

打印列表中的最后一项:

print (a[-1])

根据需要设置格式。你知道吗

顺便说一句,如果您对每个排序的列表使用相同的变量名,并且还可以将键收集到一个列表中,您可以根据输入选项对其进行索引,那么您可以使代码变得更短。你知道吗

请尝试以下操作:

# To get the first value of the list for the city, use pair[1][0].
# You can get the second value of the list for the city by using pair[1][1]
# An so on...

>>> d = {pair[0]: [pair[1][0]] for pair in sorted(cityRevenues.items(), key=lambda x: x[1][3])}
>>> list(d.items())
[('Gadsden', [45]), ('Athens', [40]), ('Anniston', [56]), ('Alabaster', [40]), ('Florence', [55]), ('Auburn', [55]), ('Decatur', [44])]

相关问题 更多 >