0

This question looks like a other stackoverflow question: Is it possible to sort two lists(which reference each other) in the exact same way?

For me the solution from this question does not work. Error: TypeError: '<' not supported between instances of 'Card' and 'Card'

I have two lists in Python. List 1 stores the number of occurences of the card. List 2 stores the card objects. An example of this I show below. This lists are directly referenced by each other. The lists are already sorted based on the rank of the card (from big to small, from ace to deuce).

cardCountList = [1, 1, 3, 3, 2]
cardList = [aceCardObject, queenCardObject, tenCardObject, eightCardObject, deuceCardObject]

What I want is to sort both lists based on the numbers in the cardCountList. But the order of the duplicate numbers must stay the same. So the end result should be:

cardCountList = [3, 3, 2, 1, 1]
cardList = [tenCardObject, eightCardObject, deuceCardObject, aceCardObject, queenCardObject]

Below code gives me the error:

 cardCountList, cardList  = zip(*sorted(zip(cardCountList, cardList )))
 cardCountList, cardList  = (list(t) for t in zip(*sorted(zip(cardCountList, cardList ))))
user7432713
  • 197
  • 3
  • 17
  • Does this work: `zip(*sorted(zip(cardCountList, cardList), key=lambda x: x[0]))` – Hampus Larsson Mar 25 '20 at 10:06
  • 1
    You can do what @HampusLarsson suggested and sort by the first item in the zipped cards, *however* what you **really** should do is make your Card objects directly comparable. [See this answer I wrote a while ago for some inspiration on that](https://stackoverflow.com/a/42399384/5014455) – juanpa.arrivillaga Mar 25 '20 at 10:09
  • 3
    You need to provide custom comparators in your card class. `__le__ , __lt__, __ge__, __gt__` == lesser_equal, lesser_then, greater_equal, greater_then, etc. methods – Patrick Artner Mar 25 '20 at 10:09
  • @juanpa.arrivillaga Actually, the card comparability is explicitly not needed here, as the sorting should occur solely based on on the counts and be stable wrt to the cards. – user2390182 Mar 25 '20 at 10:16
  • @schwobaseggl sure, notice the op stated "The lists are already sorted based on the rank of the card (from big to small, from ace to deuce)." Reading between the lines, admittedly, I believe that implementing comparisons for the Card objects would simplify whatever it is the op is actually doing. Probably, then they could just do `Counter(list_of_card_objects).most_common()` for example... – juanpa.arrivillaga Mar 25 '20 at 10:19

1 Answers1

0

You can do the following:

cardCountList = [1, 1, 3, 3, 2]
cardList = "ace queen ten eight deuce".split()

cardCountList, cardList = map(
    list, 
    zip(*sorted(zip(cardCountList, cardList), key=lambda x: x[0], reverse=True))
)

This zips the two lists together, sorts the resulting pairs by the first element, and unpacks them back.

cardCountList
# [3, 3, 2, 1, 1]
cardList
# ['ten', 'eight', 'deuce', 'ace', 'queen']
user2390182
  • 72,016
  • 6
  • 67
  • 89