1

How can I get a random pair from a dict? I'm making a game on black jack so user will get a random pair from

deck_of_cards = {'A':11,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'10':10,'J':10,'Q':10,'K':10}

and it will get stored in a dictionary

player_deck = {}

How can I do this?

mvrht
  • 33
  • 1
  • 7
  • Remember player_deck needs to be dictionary – mvrht Jun 22 '17 at 12:16
  • `player_card = [] player_card += random.choice(dict(d.items()) print player_card` – mvrht Jun 22 '17 at 12:19
  • but it works in list – mvrht Jun 22 '17 at 12:19
  • Possible duplicate of [How to get a random value in python dictionary](https://stackoverflow.com/questions/4859292/how-to-get-a-random-value-in-python-dictionary) – Arun Jun 22 '17 at 12:20
  • 2
    just wanted to point out that 'A' isn't always 11 in black jack... I hope your program deals with that later on. Also, it seems superfluous to build this dict at all, where you can simply do `deck='23456789TJQKA'` choose a random character and handle values later – Ofer Sadan Jun 22 '17 at 12:23
  • i too thought of this but later assigning values will be too much of if and else so i thought why not use dictionary – mvrht Jun 22 '17 at 12:28
  • 1
    yeah A's value depend on player @OferSadan i am thinking that after it goes greater than 21 i will replace the value of A with 1 – mvrht Jun 22 '17 at 12:29

5 Answers5

3

Use random.choice()

import random
player_deck = {}
deck_of_cards = {'A':11,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'10':10,'J':10,'Q':10,'K':10}

key, value = random.choice(list(deck_of_cards.items()))
player_deck[key] = value

Or in case you want key and value directly into a dictionary, you can do it like this

player_deck = dict([random.choice(list(deck_of_cards.items()))])
fujy
  • 5,168
  • 5
  • 31
  • 50
1

Use random.choice

import random
player_deck = {}
deck_of_cards = {'A':11,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'10':10,'J':10,'Q':10,'K':10}
d = random.choice(list(deck_of_cards.items()))
player_deck.update(d)
print(player_deck)
{'9': 9}
akash karothiya
  • 5,736
  • 1
  • 19
  • 29
1

It's not really clear how you want to simulate a deck with this dict.

If you just use random.choice multiple times, you might get the same card twice, which probably shouldn't happen.

You could create a whole deck (as a list, not as a dict), shuffle it, draw a card (thus removing it from the deck), and check its value. Defining a new Card class isn't too hard with namedtuple, and it will make it easier to work with afterwards (Thanks to @MaartenFabré for the comment):

# encoding: utf-8
import random
from collections import namedtuple


class Card(namedtuple('Card', ['face', 'color'])):
    colors = ['♠', '♥', '♦', '♣']
    faces = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
    values = dict(zip(faces, [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]))

    def __repr__(self):
        return self.face + self.color

    def value(self):
        return Card.values[self.face]

    @staticmethod
    def all():
        return [Card(face, color)
                for color in Card.colors for face in Card.faces]

deck = Card.all()

print(deck)
# ['A♠', '2♠', '3♠', '4♠', '5♠', '6♠', '7♠', '8♠', '9♠', '10♠', 'J♠', 'Q♠', 'K♠', 'A♥', '2♥', '3♥', '4♥', '5♥', '6♥', '7♥', '8♥', '9♥', '10♥', 'J♥', 'Q♥', 'K♥', 'A♦', '2♦', '3♦', '4♦', '5♦', '6♦', '7♦', '8♦', '9♦', '10♦', 'J♦', 'Q♦', 'K♦', 'A♣', '2♣', '3♣', '4♣', '5♣', '6♣', '7♣', '8♣', '9♣', '10♣', 'J♣', 'Q♣', 'K♣']

random.shuffle(deck)

print(deck)
# ['9♣', '4♠', 'J♥', '9♦', '10♠', 'K♣', '8♥', '3♣', 'J♣', '10♦', '8♦', 'A♣', '7♦', '3♠', '7♠', 'Q♣', '7♥', 'Q♦', 'A♦', '9♥', '2♠', '7♣', '6♦', '4♣', 'Q♠', '3♥', 'K♠', '6♣', '5♦', '4♥', '5♣', '2♣', '2♥', '6♥', '8♠', '2♦', '4♦', '8♣', 'K♦', '10♥', 'K♥', '5♠', 'J♦', '5♥', 'A♥', '9♠', '6♠', 'Q♥', '10♣', 'A♠', '3♦', 'J♠']

a_card = deck.pop()
print(a_card)
# J♠
print(a_card.face)
# J
print(a_card.color)
# ♠
print(a_card.value())
# 10
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
  • 'dict' object is not callable – mvrht Jun 22 '17 at 12:35
  • on line `values = dict(zip(faces, [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]))` – mvrht Jun 22 '17 at 12:35
  • @mvrht: Yes, I use a different data type than in your question. A deck shouldn't be a `dict` in my humble opinion. – Eric Duminil Jun 22 '17 at 12:37
  • what should it be ? – mvrht Jun 22 '17 at 12:40
  • @mvrht: A deck could be a list with 52 cards. You can shuffle the list and draw the first card, just like you would with a deck. You can keep a dict to get the values for each card. – Eric Duminil Jun 22 '17 at 12:42
  • `TypeError Traceback (most recent call last) in () 1 colors = ['spades', 'hearts', 'diamonds', 'clubs'] 2 faces = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] ----> 3 values = dict(zip(faces, [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10])) 4 deck = [face + ' of ' + color for color in colors for face in faces] 5 TypeError: 'dict' object is not callable ` – mvrht Jun 22 '17 at 12:48
  • @mvrht: Ahhhhh. Did you overwrite the function `dict()` with a variable called `dict`? That's one of the reasons you shouldn't do so ;) – Eric Duminil Jun 22 '17 at 12:52
  • Could you explain in detail this line `values = dict(zip(faces, [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]))` – mvrht Jun 22 '17 at 12:58
  • and this line `color for color in colors for face in faces` – mvrht Jun 22 '17 at 12:58
  • @mvrht: We have a list with 13 elements and a list with 4 elements. The double list comprehension creates a list with 52 elements, where every element from one list has been mixed with every element from the other list. It's equivalent to two nested for loops. – Eric Duminil Jun 22 '17 at 13:01
  • `dict(zip(faces, [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]))` just recreates your dict from a list of keys and a list of values. – Eric Duminil Jun 22 '17 at 13:02
  • Thanks Buddy ! You are life saver – mvrht Jun 22 '17 at 13:03
  • 1
    Instead of `deck = [face + ' of ' + color for color in colors for face in faces]` I would do `deck = list(itertools.product(colors, faces))` to get a list of tuples. How you represent them 'x of y' or another way can be done on another level, but this way you can later check the color or face easier. A separate class (or `namedTuple`) might be easier even – Maarten Fabré Jun 22 '17 at 13:10
  • @MaartenFabré: I tried to integerate your comment. Any suggestion for improvement is welcome! – Eric Duminil Jun 22 '17 at 14:10
0

You can do something like this:

import random

deck_of_cards = {'A':11,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'10':10,'J':10,'Q':10,'K':10}
player_deck = dict(random.sample(deck_of_cards.items(),1))

where I am basically asking for '1' random sample from the dictionary and typecasting the output to a dictionary (as it returns a list).

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Rudresh Panchal
  • 980
  • 4
  • 16
0

You can use random.choice:

import random
player_deck=[]
player_deck.append(random.choice(list(deck_of_cards.items())))
print(player_deck)
Nimantha
  • 6,405
  • 6
  • 28
  • 69
ammy
  • 618
  • 1
  • 5
  • 13