Вы можете создать все карты из 2 списков (масти, лица).
Вы можете сделать перемешанную копию колоды или взять 5 случайных карт без замены (random.sample):
import itertools
import random
suits = ['heart', 'tiles', 'clubs', 'pikes']
faces = ['A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K']
deck = set(itertools.product(faces, suits))
# get a mixed sample of all cards, use get first 5 cards
mixed = random.sample(deck, len(deck)) # get sample of all cards from deck (==shuffled copy)
five = mixed[:5]
print(five)
# to remove the just drawn cards from availables:
mixed = mixed[5:] # to remove the drawn cards for your mixed copy
# draw 7 non-repeated cards from deck
seven = random.sample(deck, 7) # get 7 unique cards directly from deck
print(seven)
Выход:
# five
[('6', 'pikes'), ('T', 'heart'), ('Q', 'pikes'), ('3', 'tiles'), ('J', 'clubs')]
# seven
[('A', 'tiles'), ('7', 'tiles'), ('8', 'tiles'), ('3', 'heart'), ('9', 'pikes'),
('6', 'heart'), ('T', 'clubs')]
Deckbuilding вдохновлен: codereview texas-holdem-poker-game
Взятие из предварительно * перетасованной копии deck
облегчает раздачу карт. Просто возьмите с фронта, чтобы «иметь дело» и list.pop (0) этот элемент.
Доку:
Продукт Itertools является кратным для цикла:
d = []
for s in suits:
for f in faces:
d.append((s,f))
print(d)
[('heart', 'A'), ('heart', '2'), ('heart', '3'), ('heart', '4'), ('heart', '5'),
('heart', '6'), ('heart', '7'), ('heart', '8'), ('heart', '9'), ('heart', 'T'),
('heart', 'J'), ('heart', 'Q'), ('heart', 'K'), ('tiles', 'A'), ('tiles', '2'),
('tiles', '3'), ('tiles', '4'), ('tiles', '5'), ('tiles', '6'), ('tiles', '7'),
('tiles', '8'), ('tiles', '9'), ('tiles', 'T'), ('tiles', 'J'), ('tiles', 'Q'),
('tiles', 'K'), ('clubs', 'A'), ('clubs', '2'), ('clubs', '3'), ('clubs', '4'),
('clubs', '5'), ('clubs', '6'), ('clubs', '7'), ('clubs', '8'), ('clubs', '9'),
('clubs', 'T'), ('clubs', 'J'), ('clubs', 'Q'), ('clubs', 'K'), ('pikes', 'A'),
('pikes', '2'), ('pikes', '3'), ('pikes', '4'), ('pikes', '5'), ('pikes', '6'),
('pikes', '7'), ('pikes', '8'), ('pikes', '9'), ('pikes', 'T'), ('pikes', 'J'),
('pikes', 'Q'), ('pikes', 'K')]