You can gather your data in a dictionary with the user_id as the key and the item_ids in a list as the value
import collections
user_ids = [1, 2, 3, 1, 2]
item_ids = [8, 9, 10, 5, 8]
data = collections.defaultdict(list)
for key, value in zip(user_ids, item_ids):
data[key].append(value)
The result is defaultdict(<class 'list'>, {1: [8, 5], 2: [9, 8], 3: [10]})
.
Now we can loop over the dictionary and get a random item from the list.
import random
result = [(key, random.choice(value)) for key, value in data.items()]
The result is [(1, 8), (2, 9), (3, 10)]
(or [(1, 8), (2, 8), (3, 10)]
or whatever the randomization will give us).
Some more information concerning the defaultdict
. This kind of dictionary will create a default item if it doesn't exist. The default is given as a parameter when creating the defaultdict
. Using a standard dict
we have to handle the creation of the entry ourselves.
This is how it would be done manually:
user_ids = [1, 2, 3, 1, 2]
item_ids = [8, 9, 10, 5, 8]
data = dict()
for key, value in zip(user_ids, item_ids):
if key not in data:
data[key] = []
data[key].append(value)