I have a dictionary like this:
user_dict = {
user1: [(video1, 10), (video2, 20), (video3, 1)]
user2: [(video1, 4), (video2, 8), (video6, 45)]
...
user100: [(video1, 46), (video2, 34), (video6, 4)]
}
(video1, 10) means (videoid, number of request).
Now I want to randomly choose 10 users and do some calculation like
- calculate number of videoid for each user.
- sum up the number of requests for these 10 random users, etc.
Then I need to increase the random number to 20, 30, 40 respectively.
But random.choice can only choose one value at a time, right? How to choose multiple keys and the list following each key?
That’s what
random.sample()is for:This can be used to choose the keys. The values can subsequently be retrieved by normal dictionary lookup:
Alternatively, you can directly sample from
d.items().