Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check all the elements in a list that has a specific requirement?

Tags:

python

list

I'm doing a homework about a heart game with different version. It says that if we are given a list mycards that contains all the cards that player currently hold in their hands. And play is a single card that representing a potential card.And if all their cards contain either HEART(H) or QUEEN OF SPADES(QS) it is going to return True.

For example

>>> mycards= ['0H','8H','7H','6H','AH','QS']
>>> play = ['QS']

It will return True

this is what I have tried

if play[1] == 'H':
    return True
if play == 'QS':
    return True
else:
    return False

But I think my codes just check one QS and one H in the list. How to make the codes that contain all either QS or H?

like image 586
Erika Sawajiri Avatar asked May 24 '13 06:05

Erika Sawajiri


1 Answers

Your description maps directly to the solution:

Edited for clarity:

mycards= ['0H','8H','7H','6H','AH','QS'] 
all((x == 'QS' or 'H' in x) for x in mycards)
#  True
like image 159
Thomas Fenzl Avatar answered Nov 08 '22 01:11

Thomas Fenzl