1

I have a list of strings that represents playing cards. Ranks can be

A23456789TJQK

Here is my list

# L2
['5', '6', '7', 'A', 'K']

And I'm trying to make a function to call with the key in the sorted function to return the card rank numerical values in order. So here is my function:

def sortYo():
   myDict = {'A': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'T': 10, 'J': 10, 'Q': 10, 'K': 10}

And here is my call:

L4 = sorted(L2, key = sortYo())

But my L4 ends up the same:

# L4
['5', '6', '7', 'A', 'K']

Why isn't my sortYo() function changing the values? Thank you in advance.

EDIT:

from collections import Counter
from itertools import combinations
import sys


myList = sys.argv
myList.pop(0)
Suits = ['H', 'S', 'D', 'C']
Nums = ['A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K']
SuitsTwo = {'♥': 'H', '♦': 'D', '♣': 'C', '♠': 'S'}
print(myList)
L1 = [] # Suits
L2 = [] # Ranks
L3 = [i for i in myList[0]] # Cut
for i in myList:
   for x in i:
      if x in SuitsTwo:
         L1.append(SuitsTwo[x])
      elif x in Suits:
         L1.append(x)
      else:
         L2.append(x)

print('L1:'
print(L1)
print('L2:')
print(L2)
print('L3:')
print(L3)
score = 0
# straight
sC = 0
myDict = {'A': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9\
': 9, 'T': 10, 'J': 10, 'Q': 10, 'K': 10}

L4 = sorted(L2, key = lambda x: myDict.get(x,11))
print('L4')
print(L4)

#RUN
python3 as13.py AS 5H 6C 7S KH
['AS', '5H', '6C', '7S', 'KH']
L1:
['S', 'H', 'C', 'S', 'H']
L2:
['A', '5', '6', '7', 'K']
L3:
['A', 'S']
L4
['A', '5', '6', '7', 'K']
0
1
  • 1
    ... Because you don't return anything. Commented May 24, 2018 at 1:27

2 Answers 2

3

Try using a lambda function for the sorting key:

myDict = {'A': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, 
          '9': 9, 'T': 10, 'J': 10, 'Q': 10, 'K': 10}

L4 = sorted(L2, key=lambda x: myDict.get(x, 11))

This will sort by the values associated to each key in myDict. It will also place any value in L2 that is not in the keys of myDict at the end of the sorted list by assigning a default value of 11 to unknown keys.

If you don't have to worry about items in L2 that are not in the keys of myDict, you can also use:

L4 = sorted(L2, key=myDict.get)
Sign up to request clarification or add additional context in comments.

7 Comments

Remove the lambda, just let key=myDict.get
Good point, but you can't include the default value. I will add it.
I dont think you should expect a missing value, we are talking about cards, we know all the possible values
I tried this method and the other one but I'm still not getting anything happening. Should I edit the question to show what's happening?
@Coder117 well it worked, the list was already sorted, remember your ace is worth 1
|
3
sorted(..., key='A23...JQK'.index)

4 Comments

Fortunately there aren't any (10 uses "T" instead).
@IgnacioVazquez-Abrams Thanks. I'm still not getting anything happening though. You mentioned in another comment that I'm not returning anything. What am I supposed to return in the sortYo() function? Am I supposed to return myDict?
You're supposed to return the desired key for the value that gets passed to it.
I tried returning myDict[i] but I'm still not getting anything. Any ideas?

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.