I want to add probabilities to each item in a list, where that list is in another list.
Some psuedo-code:
myList = [ [a, b, c, d], [e, f, g, h], [i, j, k, l], [m, n, o], [p, q, r], [s, t, u] ]
probabilities = [ [0.6, 0.3, 0.075, 0.025], [0.6, 0.3, 0.075, 0.025], [0.6, 0.3, 0.075, 0.025], [0.55, 0.35, 0.1], [0.55, 0.35, 0.1], [0.55, 0.35, 0.1] ]
Is there any way to do achieve this?
Further:
My need for this is to create another list that would look similar to the below...
newList = [ [b, e, k, o, p, s], [a, f, i, m, r, t], ... etc. ]
where each element was chosen randomly given the probabilities, and no two list in newList are the same. Which I am not sure is achievable.
My code so far:
layers = [list(Path(directory).glob("*.png")) for directory in ("dir1/", "dir2/", "dir3/", "dir4/", "dir5/", "dir6/")]
list_of_prob = [[0.6, 0.3, 0.075, 0.025], [0.6, 0.3, 0.075, 0.025], [0.6, 0.3, 0.075, 0.025], [0.6, 0.3, 0.1], [0.6, 0.3, 0.1], [0.6, 0.3, 0.1]]
rwp = [choices(layers, list_of_prob, k=????)]
rand_combinations = [([choice(k) for k in layers]) for i in choice_indices]
I am not entirely sure what k would be in choices(), ex. number of lists or number of total elements in the lists. Layers is a list of image paths, .pngs, which is identical to the format of "myList" provided above in pseudo code (4 images in dir1, 4 images in dir2, 4 images in dir3, 3 in dir4, 3 in dir5, 3 in dir6).
I already have code to iterate through a list and create random images, but I want some of the images to only be generated x% of the time. Hence my original question. Sorry if I just complicated things, I tried to simplify it.
I converted myList
to strings just to make things easy.
This will create combinations and append them to newList
, disregarding any combinations that already exist in newList
While loop ends when length of newList
is equal to the length of myList
import random
myList = [['a', 'b', 'c', 'd'],
['e', 'f', 'g', 'h'],
['i', 'j', 'k', 'l'],
['m', 'n', 'o'],
['p', 'q', 'r'],
['s', 't', 'u']]
probabilities = [[0.6, 0.3, 0.075, 0.025],
[0.6, 0.3, 0.075, 0.025],
[0.6, 0.3, 0.075, 0.025],
[0.55, 0.35, 0.1],
[0.55, 0.35, 0.1],
[0.55, 0.35, 0.1]]
newList = []
def random_list():
combo = []
for d, p in zip(myList, probabilities):
choice = random.choices(d, p)
combo.append(''.join(choice))
return combo
while len(newList) < len(myList):
a = random_list()
if a not in newList:
newList.append(a)
Results of newList:
[['b', 'f', 'k', 'm', 'q', 's'],
['a', 'e', 'j', 'm', 'q', 't'],
['b', 'f', 'k', 'm', 'q', 'u'],
['a', 'f', 'i', 'm', 'p', 's'],
['a', 'e', 'i', 'n', 'p', 't'],
['b', 'f', 'k', 'm', 'r', 'u']]