0

I have a string called hand = ["KC", "9D", "10S", "jH","11H", "0S", "HC", "Q2S", "100D", "1C", "2D2"]. I only want ["KC", "9D", "10S", "jH"] to return True and the rest of the string to return False. How would I do that?

I've currently written this

import re
def checkCard():
     hand = ["KC", "9D", "10S", "jH","11H", "0S", "HC", "Q2S", "100D", "1C", "2D2"]
     stack = map(bool, hand)
     print(list(stack))  
checkCard()
4
  • 1
    hand is not a string it's a list of strings and what do you mean by return your function doesn't return anything you are just printing Commented May 28, 2020 at 5:05
  • Oh sorry ahha this is my first year doing python, so my wording's all over the place. I'm required to use the map function tool to map my checkCard function onto the list. From there, I turn it into a list and display it. From this, I'm supposed to see a list of True and False values. Unrelated, I then to write a loop to display the card value and the corresponding True or False value at the same position as the card string in the list. Commented May 28, 2020 at 5:31
  • can you add the expected output Commented May 28, 2020 at 5:32
  • output should look something like this: "KC", "9D", "10S", "jH","11H", "0S", "HC", "Q2S", "100D", "1C", "2D2" "True", "True","True","True","False","False","False","False","False","False","False" Commented May 28, 2020 at 5:51

5 Answers 5

2

You can try

def checkCard():
    hand = ["KC", "9D", "10S", "jH","11H", "0S", "HC", "Q2S", "100D", "1C", "2D2"]
    true_vals = set(["KC", "9D", "10S", "jH"])
    stack  = map(lambda val: val in true_vals, hand)
    print(*hand)
    print(*stack)
checkCard()

Output

KC 9D 10S jH 11H 0S HC Q2S 100D 1C 2D2
True True True True False False False False False False False

The map function will check each value in hand if it's in a set of true_vals the conversion to set is for performance issues since searching in a set is faster then in a list.
Then printing the hand and stack by unpacking them in a print statement.

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

1 Comment

Thank you this was exactly what I was looking for!
1

You can do this with the map function like so:

def checkCard():
     hand = ["KC", "9D", "10S", "jH","11H", "0S", "HC", "Q2S", "100D", "1C", "2D2"]
     stack = map(lambda x: x in ["KC", "9D", "10S", "jH"], hand)
     print(list(stack))

Comments

1
# import re
def checkCard():
    hand = ["KC", "9D", "10S", "jH","11H", "0S", "HC", "Q2S", "100D", "1C", "2D2"]
    true_vals = ["KC", "9D", "10S", "jH"]
    bool_op = list(map(lambda val: True if val in true_vals else False, hand))

    print(bool_op)  
checkCard()

Output:

[True, True, True, True, False, False, False, False, False, False, False]

2 Comments

I have to keep the map function in there to meet the requirements of my practical, so how would i keep it in there?
Updated to use map function
0

try this, map(func, iterable)

>>> hand = ["KC", "9D", "10S", "jH","11H", "0S", "HC", "Q2S", "100D", "1C", "2D2"]
>>> 
>>> true_vals = ["KC", "9D", "10S", "jH"] 
>>> 
>>> list(map(lambda x : True if x in true_vals else False, hand))

[True, True, True, True, False, False, False, False, False, False, False]

Comments

0

map and lambda and vonversion to list is not even needed at all to achieve what you want. The most pythonic and also easiest/simplest solution is making an in check in a list comprehension:

def checkCard():
    hand = ["KC", "9D", "10S", "jH","11H", "0S", "HC", "Q2S", "100D", "1C", "2D2"]
    true_values = ["KC", "9D", "10S", "jH"]
    stack = [v in true_values for v in hand]  # check for inclusion in a list comprehension
    print(stack)  
checkCard()

Comments

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.