Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23305

Create deck of multiple decks of cards using list comprehension [duplicate]

$
0
0

I've created a Card object:

class Card:    def __init__(self, rank, suit, d=0):         self.rank = rank        self.suit = suit        self.deck_index = d

I can make a list of Cards that represents a standard deck, using list comprehension:

ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A']suits = ['hearts', 'clubs', 'diamonds', 'spades']deck = ([Card(r, s) for r in ranks for s in suits] +        [Card('Joker', 'black'), Card('Joker', 'red')])

I'd like to create a list of Cards that represents N decks combined, using list comprehension.


I can do it with a for loop:

decks = []for i in range(N):    decks += ([Card(r, s, d=i) for r in ranks for s in suits] +              [Card('Joker', ' black', i), Card('Joker', 'red', i)])

But if I write this loop as a list comprehension, I get a list of lists, instead of a list of cards:

decks = [[Card(r, s, d=i) for r in ranks for s in suits] +         [Card('Joker', 'black', i), Card('Joker', 'red', i)]          for i in range(N)]

I tried putting the target in parentheses and unpacking it with *. I got the error:Iterable unpacking cannot be used in comprehension

I could use list comprehension for the deck without Jokers and add the Jokers separately with their own list comprehensions:

decks = ([Card(r, s, i) for r in ranks for s in suits for i in range(N)] +         [Card('Joker', 'black', i) for i in range(N)] +         [Card('Joker', 'red', i) for i in range(N)])

But this redundantly specifies the loop three times.

It also puts Jokers at the end, instead of preserving the original order with Jokers next to the cards from their own deck. This point isn't too important because I shuffle in my next step, but I would like to know if it's possible to combine the decks with order preserved using list comprehension.


I'd state the general problem like this:

lst = lst1 + lst2 is a list of elements

lsts = [lst1(i) + lst2(i) for i in range(N)] is a list of lists

How can I use list comprehension to create a list of elements in lst1(i) + lst2(i) for i in range(N)?

The solution's likely available online but I don't know what words to search for. "Combine sums of lists using list comprehension" gives list comprehension tutorials and how to sum lists elementwise.One option I found is to let it make a list of lists and then flatten that using sum(lst,[]) (slow) or list(itertools.chain.from_iterable(lst)) (fast). But can I avoid making a list of lists?


Viewing all articles
Browse latest Browse all 23305

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>