Sort a list with items containing dictionary values

I am trying to create a sorting system with ranking cards, and their values ​​are obtained from a separate dictionary. In a simple deck of 52 cards, we have a rank from 2 to Ace, in this case I need a ranking system, where 0 is 10, J is 11, Q is 12, K is 13, A is 14 and 2 is 15, where 2 the largest ranked. The fact is that if there is a list where I want to sort the ranking cards in ASCENDING order in accordance with the numbering system, how to do it?

For example, here is a list,, [3,5,9,7,J,K,2,0]I want to sort the list into [3,5,7,9,0,J,K,2]. I also made a dictionary for the numbering system like {'A': 14, 'K': 13, 'J': 11, 'Q': 12, '0': 10, '2': 15}.

THANK

+3
source share
2 answers

:

    sorted(list_for_sorting, key=dictionary_you_wrote.__getitem__)

:

    sorted_list = sorted(unsorted_list, key = lambda k: your_dictionary[k])

(, ), . , lamda , .

- _ .keys(), . ,

    sorted(unsorted_list, key = lambda k: your_dictionary[k] if k in your_dictionary.keys() else k)
+1

sorted(list_for_sorting, key=dictionary_you_wrote.__getitem__)

?

0

All Articles