0

So I am creating a card game in python. For the part of the game I an currently working on, I need to check an array of playing cards to see if there are any pairs. A pair is considered two cards with the same value and color. For example, king of hearts and king of diamonds are a pair, but king of hearts and king of clubs aren't.

I need to return a new list with all of the pairs removed.

Suppose we have

list = ['9♠', '5♠', 'K♢', 'A♣', 'K♣', 'K♡', '2♠', 'Q♠', 'K♠', 'Q♢', 'J♠', 'A♡', '4♣', '5♣', '7♡', 'A♠', '10♣', 'Q♡', '8♡', '9♢', '10♢', 'J♡', '10♡', 'J♣', '3♡']

The result should be:

list without pairs = ['10♣', '2♠', '3♡', '4♣', '7♡', '8♡', '9♠', '9♢', 'A♣', 'A♡', 'A♠', 'J♠', 'J♡', 'J♣', 'K♢', 'K♣', 'K♡', 'K♠', 'Q♠']

I currently have this code:

import random
result=[]

for i in range(len(l)):
    if '♣' in l[i]:
        pass
    elif '♠' in l[i]:
        pass
    elif '♡' in l[i]:
        pass
    elif '♢' in l[i]:
        pass

random.shuffle(result)
return result
Rish
  • 804
  • 8
  • 15
Patrick
  • 29
  • 4
  • 1
    First can you write a function that checks if two card are in the same pair? – user202729 Oct 22 '21 at 23:41
  • 1
    Welcome to Stack Overflow. Please read [ask] and https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question and note that this is *not a discussion forum*. If you really don't have any idea where to start, then you don't have a question appropriate for the site. Please try Reddit or Quora. – Karl Knechtel Oct 22 '21 at 23:43
  • Umm...`a new list with all pairs removed`? Why not a list of a set...i.e. `list(set(YOUR_LIST))`?...sets are kind of meant for removing duplicates – ViaTech Oct 22 '21 at 23:45
  • 2
    Try to avoid using builtin `list` as your variable name. – Daniel Hao Oct 23 '21 at 00:25
  • Can you explain why are you removing `9♠` (9 of spade) and `9♢` (9 of diamond) from your output list along with some other non matching pairs? They do not belong to same color so according to your problem statement, they should be in the output. – Rish Oct 23 '21 at 02:47

1 Answers1

0
cards_list = [
    "9♠",
    "5♠",
    "K♢",
    "A♣",
    "K♣",
    "K♡",
    "2♠",
    "Q♠",
    "K♠",
    "Q♢",
    "J♠",
    "A♡",
    "4♣",
    "5♣",
    "7♡",
    "A♠",
    "10♣",
    "Q♡",
    "8♡",
    "9♢",
    "10♢",
    "J♡",
    "10♡",
    "J♣",
    "3♡",
]    

SAME_COLORS = {"♢": "♡", "♡": "♢", "♠": "♣", "♣": "♠"}
    
list_without_pairs = []
for card in cards_list:
    if card[:-1] + SAME_COLORS[card[-1]] in cards_list:
        continue
    else:
        list_without_pairs.append(card)

print(list_without_pairs)

Output:

['9♠', '2♠', 'Q♠', 'A♡', '4♣', '7♡', '10♣', '8♡', '9♢', 'J♡', '3♡']

This work exactly the same but could be a little bit more confuse:

list_without_pairs = [card for card in cards_list if card[:-1] + SAME_COLORS[card[-1]] not in cards_list]
  • Thank you, before I saw this I managed to figure it out myself. My way is 10x longer and more complicated so I will probably implement this instead. – Patrick Oct 23 '21 at 02:26