К сожалению, DiscreteUniform, похоже, не может обработать содержащий случай, как
P(Eq(deck, [0, 1, 2]))
or
P(deck in [0, 1, 2])
Но вы можете сделать это самостоятельно.
from sympy import Eq
from sympy.stats import P, DiscreteUniform
suits = ('Clubs', 'Diamonds', 'Hearts', 'Spades')
numbers = ('Ace', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King')
colors = {
'Black': ['Clubs', 'Spades'],
'Red': ['Diamonds', 'Hearts']
}
def get_card_name(suit, number):
return '{number}Of{suit}'.format(number=number, suit=suit)
cards = {}
for sidx, suit in enumerate(suits):
for nidx, number in enumerate(numbers):
name = get_card_name(suit, number)
cards[name] = sidx * 13 + (nidx + 1)
def flatten(list):
return reduce(lambda l, l2: l + l2, list, [])
def lookup_card_id(names):
if isinstance(names, list) or isinstance(names, tuple):
return flatten([lookup_card_id(x) for x in names])
if names in suits:
names_in_suits = [get_card_name(names, x) for x in numbers]
return lookup_card_id(names_in_suits)
if names in numbers:
names_in_numbers = [get_card_name(x, names) for x in suits]
return lookup_card_id(names_in_numbers)
if names in colors:
return lookup_card_id(colors[names])
return [cards[names]]
def prob_pick_card(name):
deck = DiscreteUniform('Cards', list(range(1, 53)))
card_id = lookup_card_id(name)
return sum([P(Eq(deck, x)) for x in card_id])
print(prob_pick_card('AceOfClubs')) # 1/52
print(prob_pick_card('Ace')) # 1/13
print(prob_pick_card('Clubs')) # 1/4
print(prob_pick_card('Black')) # 1/2