2

Ok so I have a dictionary:

stock = {
 'GOOG':520.54,
 'FB': 76.45,
 'YHOO':39.28,
 'AMZN': 306.21,
 'AAPL': 99.76,
}

I would like it to print a random number of Keys with Values (Between 2-4). Here is an example of what I did for a list to give you an idea.

items = ['Medkit', 'Water', 'Food', 'Gold',
     'Ammo', 'Beer', 'LSD']

print(random.choices(population=items, k=random.randrange(2, 5)))

I dont know how to codes this to spew out something like this for a dictionary with values and keys.

Dezert
  • 99
  • 1
  • 6
  • Welcome to [so]! It's unclear exactly what you are looking for... can you provide a [mcve], including your expected output? – TemporalWolf Dec 01 '17 at 22:11
  • 1
    Be careful since dictionaries are unordered in Python: https://stackoverflow.com/a/4326729/5881884 – DevB2F Dec 01 '17 at 22:22
  • I think this is nearly a duplicate of https://stackoverflow.com/questions/7785672/how-to-iterate-through-dict-in-random-order-in-python – progmatico Dec 01 '17 at 22:42

3 Answers3

1

The method items() on the dict will give you the key/value pairs e.g.

>>> stock.items()
[('GOOG', 520.54), ('YHOO', 39.28), ('FB', 76.45), ('AAPL', 99.76),
('AMZN', 306.21)]

So you could try:

print(random.choices(population=list(stock.items()),k=random.randrange(2, 5)))
progmatico
  • 4,714
  • 1
  • 16
  • 27
  • In Python 3 this gives `TypeError: 'dict_items' object does not support indexing` – scrpy Dec 01 '17 at 23:13
  • `stock.items()` is acutally `dict_items[('GOOG', 520.54), ...]` in Python 3. You should indicate this in your answer. – scrpy Dec 04 '17 at 09:51
  • This method can produce duplicates, e.g. `[('GOOG', 520.54), ('YHOO', 39.28), ('GOOG', 520.54)]`. – scrpy Dec 04 '17 at 09:52
1

Updated answer (ensures no duplicates)

First get a list of the keys in the dictionary (note this needs to be converted to a list or the next step won't work). Next, use random.shuffle() to shuffle this list of keys and ensure a random order. Then simply use the first 2–4 keys in the shuffled list to create a new random dictionary, which you can print. Note that this method ensures the 2–4 random keys chosen are unique (unlike methods using random.choice() or random.choices(), which can produce duplicates).

my_keys = list(stock.keys())
random.shuffle(my_keys)
my_stock = {key: stock[key] for key in my_keys[:random.randrange(2,5)]}
print(my_stock)

Example outputs:

{'GOOG': 520.54, 'AMZN': 306.21}
{'AMZN': 306.21, 'GOOG': 520.54, 'YHOO': 39.28}
{'AAPL': 99.76, 'AMZN': 306.21, 'YHOO': 39.28}
scrpy
  • 985
  • 6
  • 23
0

If I understand your question correctly you want to get 2 to 4 random keys and their values.

What about this?

import random
for i in range(3):
    randomKey = random.choice(stock.keys())
    print(randomKey+" : "+stock[randomKey])

That's basically it. I have chosen 3 iterations of the for loop becasue you said, you want a number of 2 to 4 random keys. Of course you could check that no key appeares two times.

usedKeys = []

And later on check if your random key exists and if not, append it to the list.

halfer
  • 19,824
  • 17
  • 99
  • 186
fameman
  • 3,451
  • 1
  • 19
  • 31