[英]Adding probabilities to items in a list of lists
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.其中每个元素都是根据概率随机选择的,newList 中没有两个列表是相同的。 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.我不完全确定 choices() 中的 k 是什么,例如。 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).
Layers是图片路径列表,.pngs,与上面伪代码中提供的“myList”的格式相同(dir1中4张图片,dir2中4张图片,dir3中4张图片,dir4中3张图片,dir4中3张图片,dir5中3张图片, 3 在 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.我已经有了遍历列表并创建随机图像的代码,但我希望某些图像仅在 x% 的时间内生成。 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.我将
myList
转换为字符串只是为了让事情变得简单。
This will create combinations and append them to newList
, disregarding any combinations that already exist in newList
这将创建组合并将它们 append 到
newList
,忽略newList
中已经存在的任何组合
While loop ends when length of newList
is equal to the length of myList
当
myList
的长度等于newList
的长度时 while 循环结束
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: 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']]
So if I understood correctly, you want to take one element of each sublist of myList
based on the probabilities to get an element, and do this multiple times to get a list of lists.所以如果我理解正确的话,你想根据概率从
myList
的每个子列表中取一个元素来获取一个元素,并多次执行此操作以获取列表列表。
With Python 3, you can do:使用 Python 3,您可以:
import random
# the new set since two elements cannot be the same
result_set = set()
# the number of times you want to do it, you can change it.
for i in range(5):
# A single result as a str, hashable.
result = ""
for chars, char_probabilities in zip(myList, probabilities):
# weighted random choice
char = random.choices(chars, char_probabilities)
result += char[0]
result_set.add(result)
result_list = [list(elt) for elt in result_set]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.