因此,为了好玩和练习,我一直跟随一位大学朋友和他们的高级编程课,并处理分配给他们的任务,以便为同一个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])]
我怎样才能让特定的元组值成为唯一的打印值呢?你知道吗
因为最大的元素在列表的末尾,所以可以使用索引
-1
例如
它会给你
打印列表中的最后一项:
根据需要设置格式。你知道吗
顺便说一句,如果您对每个排序的列表使用相同的变量名,并且还可以将键收集到一个列表中,您可以根据输入选项对其进行索引,那么您可以使代码变得更短。你知道吗
请尝试以下操作:
相关问题 更多 >
编程相关推荐