2

Could you please tell me how can I generate a dictionary with 100 rows that have random number between 0 and 1 in each row as the value? For example in data frame, I can have:

   df['Rand'] = random.sample(random.random(), 100)

But I don't know how to do that for a dictionary.

user36729
  • 545
  • 5
  • 30

2 Answers2

9

I think what you want is something like:

{k: random.random() for k in range(100)}
Travis Griggs
  • 21,522
  • 19
  • 91
  • 167
  • Why to create `dict` with key from 0 to N when you can simply do it my using list? – Moinuddin Quadri Oct 17 '16 at 21:44
  • @anonymous, I think OP wanted fractional values between 0 and 1, not just 1's and 0's – Travis Griggs Oct 17 '16 at 21:46
  • @user36729, if this answer does the job for you, feel free to assign it as the answer – Travis Griggs Oct 17 '16 at 21:47
  • @TravisGriggs: I agree you on that. As it was wrong in my answer that I corrected :). But I just wanted to mention that in this scenario (based on what user desires), it should be `list` instead and not `dict` – Moinuddin Quadri Oct 17 '16 at 21:48
  • Yeah, the OP doesn't actually indicate what he plans on using as keys. I just did the simplest thing that could possibly work there, since that was unspecified. It maybe that he's actually got a list of 100 city names he's assigning random weights to, or whatever. In the example as shown, if it were stop there, I agree that a list would be the better choice. – Travis Griggs Oct 17 '16 at 21:50
  • @anonymous: Since I realized searching over dictionaries is faster, I am going to use dict not list. I have to add many other things to this dictionary as well. I am a beginner in Python, so please tell me if there is a better way to do that. – user36729 Oct 17 '16 at 23:08
  • *Searching is faster in the dict* when you need to search on some specific key. In your case, list will also be ordered. Doing `list[n]` will be faster than doing `dict[n]`. If you want to add many other things, then definitely it should be `list` *of* `dict` (you can not use dict) because `key` should be unique. Only you know what you want to achieve and implementation may vary based on your requirement. I will suggest you to read: [When to use a Dictionary, List or Set?](http://stackoverflow.com/questions/3489071/in-python-when-to-use-a-dictionary-list-or-set) (also mentioned in my answer) – Moinuddin Quadri Oct 17 '16 at 23:18
0

Firstly, it should be list and not dict. Check: In Python, when to use a Dictionary, List or Set?

In order to get the list of values, you may use list comprehension as:

>>> import random
>>> row_count = 10
>>> my_list = [random.random() for i in range(row_count)]
# Value of 'my_list':
# [0.9158936600374181, 0.8998648755500501, 0.07002867165493243, 0.6694284854833131, 0.4903966580363698, 0.9462143737260301, 0.8014661448602305, 0.47964245438139297, 0.42326131297319725, 0.77540761767324]

In order to fetch 5th item (i.e. 4th index):

>>> my_list[4]
0.4903966580363698
Community
  • 1
  • 1
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126