Home > Net >  zip() not running for a list with float values
zip() not running for a list with float values

Time:10-24

sorted(zip(all_genres, glob_sums), key = float, reverse=True)[:3]

I need to print the top 3 values for my data but I keep getting the 'float' object is not iterable error. glob_sums is a list of float values and all_genres is a list of the corresponding string values. How can I sort the list in descending order of glob_sums and then display the top 3 values along with their corresponding names?

CodePudding user response:

You can try

sorted(zip(all_genres, glob_sums), key = lambda (genre,s) : s, reverse=True)[:3]

CodePudding user response:

sorted is attempting to apply the float() to each element of the iterable. In your case each element is ("some name", some_value). We can dip inside if instead we pass a function as the key!

sorted(zip(all_genres, glob_sums), key = lambda x: float(x[1]), reverse=True)[:3]
  • Related