I am writing python code to interpret the data I collect while playing a game of clue. In a particular section of the code I am adding a list of cards to one of three different sets depending on which set corresponds to the cards in my hand. At this point I already have a list filled with 6 cards, like so...
standby_list = ['Mustard', 'Plum', 'Revolver', 'Candlestick', 'Study', 'Hall']
These are the six cards I am attempting to add to my hand. In addition, I already have a dictionary of the three players involved in the game paired with their "player number," such as...
players = {"Billy" : "Player 0", "Bob" : "Player 1", "Joe" : "Player 2"}
And finally, there are three empty sets, one set for the cards of each player...
player0_cards = set()
player1_cards = set()
player2_cards = set()
My objective is to (1) determine which of these three empty sets corresponds to the cards that are in my (Bob's) hand, and (2) add my list of six cards into that set.
The code that I have now, which to seems bad and "non-Pythonic" is as follows.
standby_list = ['Mustard', 'Plum', 'Revolver', 'Candlestick', 'Study', 'Hall']
player0_cards = set()
player1_cards = set()
player2_cards = set()
players = {"Billy" : "Player 0", "Bob" : "Player 1", "Joe" : "Player 2"}
if players['Bob'] == 'Player 0':
for card in standby_list:
player0_cards.add(card)
elif players['Bob'] == 'Player 1':
for card in standby_list:
player1_cards.add(card)
elif players['Bob'] == 'Player 2':
for card in standby_list:
player2_cards.add(card)
Who knows maybe this is the best way to code for what I want the program to do, If not please let me know.
Thanks...
One Pythonic solution is to use a dictionary to store a variable number of variables. Resist the temptation to name each and every variable. Instead, group similar variables as key-value dictionary mappings.
Note also you can use set.update instead of adding each item of a list to a set sequentially.
Here's an example. Notice that we can feed the value returned by players['Bob'] directly to the cards dictionary. Since this returns a set, we can use set.update on the result.
standby_list = ['Mustard', 'Plum', 'Revolver', 'Candlestick', 'Study', 'Hall']
players = {"Billy" : "Player 0", "Bob" : "Player 1", "Joe" : "Player 2"}
cards = {'Player 0': set(), 'Player 1': set(), 'Player 2': set()}
cards[players['Bob']].update(standby_list)
print(cards)
{'Player 0': set(),
'Player 1': {'Candlestick', 'Hall', 'Mustard', 'Plum', 'Revolver', 'Study'},
'Player 2': set()}
How about something like this
standby_list = ['Mustard', 'Plum', 'Revolver', 'Candlestick', 'Study', 'Hall']
players = {"Billy" : "Player 0", "Bob" : "Player 1", "Joe" : "Player 2"}
cards = {'Player 0': set(), 'Player 1': set(), 'Player 2': set()}
cards[players['Bob']].add(tuple(standby_list))
print (cards[players['Bob']])
# {('Mustard', 'Plum', 'Revolver', 'Candlestick', 'Study', 'Hall')}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With