【发布时间】:2019-12-20 15:14:36
【问题描述】:
我当前遇到的问题是在尝试使用集合排序方法对为保留用户名和最高分而制作的字典进行排序时发生的。 虽然我已经使用 3 个列表实现了可编辑的高分功能,但其中一个包含浮点值。一个保存浮点值+'%',另一个保存玩家的名字。由 zip(name, float value+' %') 制成的dict上的排序方法
** 将输出 9.99%
** 将 100% 的分数放在排行榜底部附近,与 10% 相同。
当前使用的方法
sorted_position = sorted(position_collection.items(), key=lambda kv: kv[1], reverse=True)
position_collection=dict(zip(name_list,score_list))
#step 4: creation of a dictionary from the name_list and score_list and sorting it by value
sorted_position = sorted(position_collection.items(), key=lambda kv: kv[1], reverse=True)
#This is the part of the code found on stack overflow it orders the dictionary by the key value I do not fully understand this code but it is causing issues.
# I got it from here - https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value
for z in range(0,len(sorted_position)):
this_tuple=tuple(sorted_position[z])
pos = z+1
# This takes the position from the amount of tuples returned. Notice the cast to tuple. What was returned was an array I think? containing all of the tuples
# When tuple = 0 it is the Name 1 is the score
if z!=len(sorted_position)-1:
#step 5: taking the tuples name and score and adding them to final_line the finished product that will be outputed.
final_line = final_line + str(pos)+". "+str(this_tuple[0])+" : "+str(this_tuple[1])+"\n"
else:
final_line = final_line + str(pos)+". "+str(this_tuple[0])+" : "+str(this_tuple[1])
result = basetext+final_line
虽然预期的记分牌应该是从高到低的分数,但回报看起来像这样
==LeaderBoard==
1. Orion Nelson : 81.395%
2. Zahara Mahoney : 7.698%
3. Lyndsey Sadler : 7.683%
4. Joe Man : 58.337%
5. Myah Chapman : 27.265%
6. Thelma Parra : 27.263%
7. Grady Amin : 22.231%
8. Elouise Stokes : 15.394%
9. Ivie Contreras : 15.392%
10. Saniiya Bolton : 15.378%
11. Farhan Gallagher : 13.336%
12. Harmony Christian : 12.503%
13. Winner Winner : 108.58%
如果有人知道发生了什么,我的想法是它只注册了前两个数字,但不知道为什么。
谢谢,
【问题讨论】:
标签: python sorting dictionary collections