I'm trying to make a abnormal deck of cards. Each card will have a color (red, green or blue), a degree (1, 2 or 3), a symbol (triangle, square or circle) and a number (1,2 or 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 the following lists containg all the possible values for the fields and an empty deck where I want the cards to get into.
colors = ["red", "green", "blue"]
degrees = ["1", "2", "3"]
symbols = ["triangle", "square", "circle"]
numbers = ["1", "2", "3"]
deck = []
I want to create a full deck with every possible card. Preferably, they would be in a random order, but that's not necessary. I know that if it was only 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 can't figure out how to do it with a symbol and a degree as well. I tried to build on more if statements to loop all the list, but that didn't work. I also don't want the same card to appear twice, and I dont want a card that doesn't follow the class rules: they must all be strutured as [color, degree, symbol, number].
Does anyone have a idea how to do this this?
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