I have 2 lists
lst1 = [0.9932, 0.9982, 0.9979, 0.9981, 0.9993, 0.9985, 0.9924, 0.9984, 0.9987, 0.9967, 0.995, 0.9932]
and
lst2 = ["Jane", "Tylor", "James", "Tom", "Smith", "Johnson", "Williams", "Jones", "Brown", "Davis", "Miller", "Wilson"]
I want to make a list which contains 3 tuples which then contain 2 items within them. The second item in each of the 3 tuple is the float point value from lst1 arranged in ascending order (top 3 values from lst1
) and the second item in the tuple is the name from lst2
corresponding to the float point from lst1
.
So essentially I want
[("Smith", 0.9993),("Brown", 0.9987),(0.9985, "Johnson")]
However, if the highest values from lst1
are the same, then I want to sort by the name.
For example: (0.9994, "Abby")
should come before (0.9994, "Bob")
I have tried:
sorted(zip(lst1, lst2), reverse=True)[:3]
which gives me
[(0.9993, "Smith"),(0.9987, "Brown"), (0.9985, "Johnson")]
i.e. the lst1
item comes before lst2
item in the tuple. If I swap lst1
and lst2
, it still does not work.
CodePudding user response:
Change the order of lst1 and lst2 in zip and use the key parameter of sorted
lst1 = [0.9932, 0.9982, 0.9979, 0.9981, 0.9993, 0.9985, 0.9924, 0.9984, 0.9987, 0.9967, 0.995, 0.9932]
lst2 = ["Jane", "Tylor", "James", "Tom", "Smith", "Johnson", "Williams", "Jones", "Brown", "Davis", "Miller", "Wilson"]
# sort in descending order by score, alphabetically by name
sorted(zip(lst2, lst1), key=lambda x: (-x[1], x[0]))[:3]
[('Smith', 0.9993), ('Brown', 0.9987), ('Johnson', 0.9985)]