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...
-
2You've not done terribly by the way!ababuji– ababuji2018年07月02日 21:18:22 +00:00Commented Jul 2, 2018 at 21:18
3 Answers 3
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()}
Comments
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')}
1 Comment
You can initialize standby_list as a set (because cards are unique), players as a list, and use list comprehension to generate player_cards to make it Pythonic. Specifying "Player 1", "Player 2" and "Player 3" is redundant because the list index itself can represent the player number.
standby_list = {'Mustard', 'Plum', 'Revolver', 'Candlestick', 'Study', 'Hall'}
players = ['Billy', 'Bob', 'Joe']
player_cards = [standby_list if player == 'Bob' else set() for player in players]
And player_cards will become:
[set(), {'Study', 'Revolver', 'Mustard', 'Plum', 'Hall', 'Candlestick'}, set()]
Your player number can then be an integer used to access the player_cards list of sets.