algorithm - How do I take a python Dictionary or List of size X and assign each element with Y random index values of the dictionary itself -


i'm not sure if best use list or dictionary algorithm. assuming use dictionary, want create dictionary of size x , randomly assign each element y index values of dictionary itself.

meaning take dictionary of size 5, , assign each of 5 elements 2 index values ranging between 1-5.

the constraints index value can not assigned own index, 2nd index can assigned values 1,3,4,5; , y must less x, in order prevent assigning duplicate index values same index.

what have far being done list rather dictionary, i'm not sure if best method. i'd keep algorithm running @ 0(n) speed well, if size of list/dictionary huge. either way, i'm at.

so, make x list of size 5. set y equal 3, meaning want each of 5 elements contain 3 index values. in for-loop create list excluding index value i'm assigning values to.

x = range(5)[::1] # [0, 1, 2, 3, 4] print(x) y = 3     assigned = []  k in range(0, len(x)):       xexcluded = [x i,x in enumerate(x) if i!=k]   # if k==3 [0, 1, 2, 4]     print("excluded: {}" .format(xexcluded))      assigned.append(list(random.sample(xexcluded, y)))     print("assigned: {}" .format(assigned)) 

sample output:

[0, 1, 2, 3, 4] excluded: [1, 2, 3, 4] assigned: [[1, 2, 3]] excluded: [0, 2, 3, 4] assigned: [[1, 2, 3], [3, 2, 4]] excluded: [0, 1, 3, 4] assigned: [[1, 2, 3], [3, 2, 4], [3, 4, 1]] excluded: [0, 1, 2, 4] assigned: [[1, 2, 3], [3, 2, 4], [3, 4, 1], [0, 1, 2]] excluded: [0, 1, 2, 3] assigned: [[1, 2, 3], [3, 2, 4], [3, 4, 1], [0, 1, 2], [2, 3, 1]] 

one thing implement someway average out index values being assigned on time, because right algorithm may assign index values more others. may more apparent when starting smaller lists, i'd imagine wont of problem when starting large list since allow randomly sampled index values better average out on time.

to balance selections, in case of example, x , y both small, naive solution checking value missing in each iteration , adding value next round add additional weighting during random sampling.

below simple workable example, without effort in efficiency optimization. may consider using other data structure set optimization. (diff in sets takes o(n). @ end may o(n^2). since aimed @ short list case, set chosen simplicity in code.)

import random  x = set(range(5)[::1]) # assume have distinct values shown in example y = 3 assigned = [] tobeweighted=none  def assigned_and_missing(fullset, index):   def sample(inlist, weighteditem, y):     if weighteditem not none:       inlist.append(weighteditem)     print("weighted item: {}" .format(weighteditem))     print("interim selection (weighted item appended): {}" .format(inlist))     randselection = set(random.sample(inlist, y))     remain = fullset - set((index+1,)) - randselection        missingitem = remain.pop()     if len(randselection) < y:       randselection.add(remain.pop())     return randselection, missingitem   return sample   k in range(0, len(x)):     weighted_random = assigned_and_missing(x, k)     xexcluded = [x i,x in enumerate(x) if i!=k]   # if k==3 [0, 1, 2, 4]      print()     print("excluded: {}" .format(xexcluded))      selection, tobeweighted = weighted_random(xexcluded, tobeweighted, y)     print("final selection: {}" .format(selection))      assigned.append(selection)     print("assigned: {}" .format(assigned))     print("item needs weighted in next round: {}" .format(tobeweighted)) 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -