0

Here is how my dictionary looks like:

{((0,), (16,)): 12.0, ((0,), (17,)): 14.0, ((1,), (16,)): 10.0, ((1,), (17,)): 13.0, ((2,), (15,)): 9.0, ((2,), (16,)): 21.0, ((2,), (17,)): 42.0, ((3,), (16,)): 13.0, ((3,), (17,)): 21.0, ((4,), (16,)): 9.0, ((4,), (17,)): 12.0, ((8,), (15,)): 7.0, ((8,), (16,)): 16.0, ((8,), (17,)): 20.0, ((9,), (16,)): 9.0, ((9,), (17,)): 13.0, ((10,), (15,)): 10.0, ((10,), (16,)): 18.0, ((10,), (17,)): 35.0, ((11,), (15,)): 10.0, ((11,), (16,)): 20.0, ((11,), (17,)): 37.0, ((12,), (15,)): 8.0, ((12,), (16,)): 16.0, ((12,), (17,)): 21.0, ((13,), (15,)): 7.0, ((13,), (16,)): 20.0, ((13,), (17,)): 25.0, ((14,), (16,)): 8.0, ((14,), (17,)): 8.0}

I'm trying to sort the dictionary based on the recommendation here:

sorted_items = [i[0] for i in sorted(sorted_items.items(),
                                     key=lambda kv: (kv, -kv[0][0], -kv[1][0]),
                                     reverse=True)]

This is the error I get:

TypeError: bad operand type for unary -: 'tuple'

How do i address this?

tandem
  • 2,040
  • 4
  • 25
  • 52
  • 3
    So what is your question? Is that not producing the desired output? Are you getting an error? Can you show an example of your desired output? – Cory Kramer Oct 21 '20 at 13:37
  • 2
    This is possibly one of the ugliest dictionaries I've ever seen and you would benefit immensely from removing inner tuples. That being said, you probably want `(kv, -kv[0][0][0], -kv[0][1][0])` – JBernardo Oct 21 '20 at 13:42
  • it is hard to parse through indeed. I'm trying to understand this code (https://github.com/GiulioRossetti/tbp-next-basket/blob/master/tbp/tars.py#L501). To do so, I need to understand what it does. – tandem Oct 21 '20 at 13:44
  • 1
    That would work if your dictionary didn't have inner tuples, like I said. E.g.: `{(0, 16): 12.0, (0, 17): 14.0,...}` – JBernardo Oct 21 '20 at 13:46
  • @JBernardo, thanks for explaining what the code codes with the example and your answer :) – tandem Oct 21 '20 at 13:52

0 Answers0