2

SO, im gonna make a abnormal deck of cards. Each card will have a color (red, green, blue), a degree(1,2,3), a symbol(triangle, square, circle) and a number.(1,2,3) I have a class that looks like this.

class card:
def __init__(self, color, degree, symbol, number):
    self.color=color
    self.degree=degree
    self.symbol=symbol
    self.number=number
def __repr__(self):
    return "(%s,%s,%s,%s)" %(self.color,self.degree,self.symbol,self.number)

I also have these lists with all the variables and a deck of cards where i want the cards.

colors=["red", "green", "blue"]
degrees=["1","2","3"]
symbols=["triangle", "square", "circle"]
numbers=["1","2","3"]
deck=[]

Now, what i wanna do is create a full deck with every possible card. Preferably they would be in a random order but its not nessecary. I know that if it was just a number and color i could easily do it this way.

deck = [card(value, color) for value in range(0, 2) for color in colors]

But, i cant figure out how to make it when im also gonna use symbol and degree as well. I tried to just build on more if statements to loop it all but that didnt work. I also doesnt want the same card to appear twice, and i dont want a card that doesnt follow the class rules, they must be strutured as [color,degree,symbol,number]

Does anyone have a idea where to go with this?

effa94
  • 31
  • 4

4 Answers4

2

Full deck with every possible card combination:

deck = [card(color, degree, symbol, number) for color in colors \
        for degree in degrees for symbol in symbols for number in numbers]

For randomizing the card order in the deck, take a look at this: Shuffling a list of objects

fabio.avigo
  • 188
  • 12
1

use product from itertools

import itertools


deck = [
    card(color, degree, symbol, number)
    for color, degree, symbol, number in
    itertools.product(colors, degrees, symbols, numbers)
]
Mohit Solanki
  • 2,012
  • 12
  • 19
0

Do you want all combinations of colors, degrees, symbols and numbers?

If so, use nested for loops:

deck = []
for color in colors:
    for degree in degrees:
        for symbol in symbols:
            for number in numbers:
                deck.append(card(color, degree, symbol, number)

# Also written as a list comprehension
deck = [
    card(color, degree, symbol, number)
    for color in colors
        for degree in degrees
            for symbol in symbols
                for number in numbers
]  # The indent is just to show how it works. For style, put them all at the same indent.

Or use itertools.product (Which can also be lazy)

deck = itertools.starmap(card, itertools.product(colors, degrees, symbols, numbers))

deck = list(deck)  # If you really need it to be a list
Artyer
  • 20,910
  • 3
  • 38
  • 60
  • I tried the first loop there, however it only gave me 29 cards, while i was expecting the entire deck to contain 81. why isnt there more of them? I couldnt get the second example there to work, since im using a main function which calls upon this function that makes the deck, and then it wouldnt recognize the deck object, same problem when i tried the itertools things. – effa94 Sep 26 '18 at 12:41
0
import itertools

identifiers = [colors, degrees, symbols, numbers]
deck = [[*i] for i in itertools.product(*identifiers)]
[['red', '1', 'triangle', '1'], ['red', '1', 'triangle', '2'], ['red', '1', 'triangle', '3'],...
vash_the_stampede
  • 4,449
  • 1
  • 6
  • 20