0

I have a list of five-character strings, each string representing a hand of five playing cards. I want to sort each string so it's in ascending order by number, followed by the cards (T,J,Q,K,A). So “Q4JTK”, "9T43A" and “T523Q” will sort to “4TJQK”, "349TA" and “235TQ” respectively. I can sort a numeric five character string using:

def sort_string(s): 
return ''.join(sorted(s))

print(sort_string("21437"))

But how to sort a string with numbers and letters? I would probably find a regular function easier to follow than a lambda function. Thanks.

3
  • why T come before K Commented Mar 29 at 16:21
  • @sahasrara62 T = 10 Commented Mar 29 at 16:22
  • oh my bad, its card game Commented Mar 29 at 16:23

2 Answers 2

6

Sorting by index in the desired order:

def sort_string(s): 
    return ''.join(sorted(s, key='23456789TJQKA'.index))

print(sort_string('Q4JTK') == '4TJQK')
print(sort_string('9T43A') == '349TA')
print(sort_string('T523Q') == '235TQ')

Attempt This Online!

Sign up to request clarification or add additional context in comments.

10 Comments

That's just what I need, but what does "key='23456789TJQKA'.index" mean? I couldn't find "key" and "index" in the documentation here: docs.python.org/3/howto/sorting.html
@Peter4075 Props to being the only one to link the python docs. Scroll to "Key Functions" in that link. Index is a method on string which does a linear search.
@Peter4075 "key" appears on that page about 40 times. There's an entire section about it. And "index" is just the position.
key defines a comparison function/method. .index is a String method which returns the index of some value in the String. If you try print('23456789TJQKA'.index('Q')) then you will see that the method gives the position to the character in the String - and hence can be used as a comparison key.
I would recommend putting the explanation in the answer.
|
1

The issue that you're seeing it that Python's sorted() on strings with numbers and letters, sorts each character by its ASCII value (uppercase letters will come before numbers), which is not how cards would work when sorted. To fix it without a lambda expression so it's easier to follow would be doing something like mapping each character to a rank (i.e. 'A" : 1, '2': 2, etc...)

def card_rank(card):
    rank_order = {
        '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
        'T': 10, 'J': 11, 'Q': 12, 'K': 13, 'A': 14
    }
    return rank_order[card]
def sort_string(s):

    return ''.join(sorted(s, key=card_rank))

So here, sorted() accepts another optional parameter - the key function. And then sorted the string based off the assigned card's rank.

If it's difficult to understand how sorted() may work, I've attached the documentation here

https://www.programiz.com/python-programming/methods/built-in/sorted

Good Luck!

3 Comments

also card_rank = rank_order.get
Using a mapping is more efficient than str.index, but you're throwing away that advantage by constantly rebuilding the dict. So do something like this instead: card_rank = {c: r for r, c in enumerate('23456789TJQKA')}.get.
dict(zip('23456789TJQKA', range(13)).get

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.