将概率添加到列表列表中的项目
Adding probabilities to items in a list of lists
我想为列表中的每个项目添加概率,其中该列表在另一个列表中。
一些伪代码:
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 = [ [b, e, k, o, p, s], [a, f, i, m, r, t], ... etc. ]
其中每个元素都是根据概率随机选择的,newList 中没有两个列表是相同的。我不确定是否可以实现。
到目前为止我的代码:
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]
我不完全确定 choices() 中的 k 是什么,例如。列表的数量或列表中的总元素数。 Layers是图片路径列表,.pngs,与上面伪代码中提供的“myList”的格式相同(dir1中4张图片,dir2中4张图片,dir3中4张图片,dir4中3张图片,dir4中3张图片,dir5中3张图片, 3 在 dir6).
我已经有了遍历列表并创建随机图像的代码,但我希望某些图像仅在 x% 的时间内生成。因此我最初的问题。对不起,如果我只是把事情复杂化,我会尽量简化它。
所以如果我没理解错的话,你想根据概率取myList
的每个子列表中的一个元素来得到一个元素,并多次这样做以获得列表的列表。
有了Python3,你可以做:
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]
为了方便起见,我将 myList
转换为字符串。
这将创建组合并将它们附加到 newList
,忽略 newList
中已经存在的任何组合
当 newList
的长度等于 myList
的长度时,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)
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']]
我想为列表中的每个项目添加概率,其中该列表在另一个列表中。
一些伪代码:
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 = [ [b, e, k, o, p, s], [a, f, i, m, r, t], ... etc. ]
其中每个元素都是根据概率随机选择的,newList 中没有两个列表是相同的。我不确定是否可以实现。
到目前为止我的代码:
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]
我不完全确定 choices() 中的 k 是什么,例如。列表的数量或列表中的总元素数。 Layers是图片路径列表,.pngs,与上面伪代码中提供的“myList”的格式相同(dir1中4张图片,dir2中4张图片,dir3中4张图片,dir4中3张图片,dir4中3张图片,dir5中3张图片, 3 在 dir6).
我已经有了遍历列表并创建随机图像的代码,但我希望某些图像仅在 x% 的时间内生成。因此我最初的问题。对不起,如果我只是把事情复杂化,我会尽量简化它。
所以如果我没理解错的话,你想根据概率取myList
的每个子列表中的一个元素来得到一个元素,并多次这样做以获得列表的列表。
有了Python3,你可以做:
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]
为了方便起见,我将 myList
转换为字符串。
这将创建组合并将它们附加到 newList
,忽略 newList
当 newList
的长度等于 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)
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']]