我有兴趣模拟关于概率分配的以下问题的样本空间:
一个人会为他的两个女儿和三个儿子雕刻南瓜。他的妻子会把每个孩子的南瓜按完全随机的顺序送去。这个人决定,一旦他为他的两个儿子雕刻了南瓜,他就会要求他的妻子雕刻剩下的南瓜。让W表示他将要雕刻的南瓜的数量。
所以W的样本空间如下所示:
sample_space=[['S','S'],
['S','D','S'],
['S','D','D','S'],
['D','S','S'],
['D','S','D','S'],
['D','D','S','S']]我想有两个名单,一个是儿子,一个是女儿:
son_list1=['S','S','S']
daughter_list1=['D','D']然后把它们按照每一个可能的顺序组合起来:
result_list1=[['S','S','S','D','D'],
['S','S','D','S','D'],
['S','S','D','D','S'],
['S','D','S','S','D'],
['S','D','S','D','S'],
['S','D','D','S','S'],
['D','S','S','S','D'],
['D','S','S','D','S'],
['D','S','D','S','S'],
['D','D','S','S','S']]我不知道在我们拥有的地方,把每个儿子和每个女儿编号,然后把它们组合起来是否更容易:
son_list2=['S1','S2','S3']
daughter_list2=['D1','D2']由此产生的列表如下:
result_list2=[['S1','S2','S3','D1','D2'],
['S1','S3','S2','D1','D2'],
['S2','S1','S3','D1','D2'],
['S2','S3','S1','D1','D2'],
['S3','S1','S2','D1','D2'],
['S3','S2','S1','D1','D2'],
...
['D2','D1','S3','S2','S1']]但是如果这个方法更简单,我就可以在result_list2生成之后去掉数字,然后删除重复序列。
无论如何,在以result_list1的形式获得结果列表之后,我可以创建一个“子计数器”,然后遍历每个列表,然后在“子计数器”达到2时停止,然后从那里删除重复以获得sample_space列表。
还有更好的逻辑吗?
发布于 2020-10-20 00:12:31
为了解决这个问题,我认为最好的解决办法是得到他雕刻每个南瓜的顺序的所有排列。
我只是使用了以下代码,用于从GeeksforGeeks获取集合的所有排列。我只是更改了一些变量的名称,以使它更加清楚。
def permutation(passed_list):
# If passed_list is empty then there are no permutations
if len(passed_list) == 0:
return []
# If there is only one element in lst then, only
# one permutation is possible
if len(passed_list) == 1:
return [passed_list]
# Find the permutations for passed_list if there are
# more than 1 characters
perm_list = [] # empty list that will store current permutation
# Iterate the input(passed_list) and calculate the permutation
for i in range(len(passed_list)):
item = passed_list[i]
# Extract passed_list[i] or item from the list. remaining_list is
# remaining list
remaining_list = passed_list[:i] + passed_list[i + 1:]
# Generating all permutations where item is first
# element
for p in permutation(remaining_list):
perm_list.append([item] + p)
return perm_list然后,您只需遍历所有的排列,并在执行过程中跟踪顺序。一旦你找到了两个儿子,你就停止了迭代,把这个顺序添加到你的样本空间中,然后进入下一个排列。
if __name__ == '__main__':
# Set of all children. It doesn't matter what order this list is in
children = ['S', 'S', 'S', 'D', 'D']
# perms is the list of all permutations of children list
perms = permutation(children)
# This set will hold the resulting sample space you are looking for
total_set = []
# For each permutation
for perm in perms:
order = [] # Contains the order of whose pumpkin he carves
son_counter = 0
for child in perm:
if child is 'S':
son_counter += 1
# Update the order
order.append(child)
if son_counter is 2:
# To keep from adding duplicate orders
if order not in total_set:
total_set.append(order)
# Reset the following two variables for the next iteration
order = []
son_counter = 0
break
print(total_set)这给了我以下输出:
[['S', 'S'], ['S', 'D', 'S'], ['S', 'D', 'D', 'S'], ['D', 'S', 'S'], ['D', 'S', 'D', 'S'], ['D', 'D', 'S', 'S']]我相信这就是你想要的答案。
如果你有什么问题请告诉我!
发布于 2020-10-20 00:33:52
您可以使用动态编程从下向上构建示例空间。例如,
def create_samples(n_sons, n_daughters):
if n_sons == 0:
# stop carving
yield []
elif n_daughters == 0:
# must carve n_sons more pumpkins
yield ['S'] * n_sons
else:
# choose to carve for a sun
for sample in create_samples(n_sons - 1, n_daughters):
yield ['S'] + sample
# choose to carve for a daughter
for sample in create_samples(n_sons, n_daughters - 1):
yield ['D'] + sample
samples = list(create_samples(2, 2))
# [['S', 'S'],
# ['S', 'D', 'S'],
# ['S', 'D', 'D', 'S'],
# ['D', 'S', 'S'],
# ['D', 'S', 'D', 'S'],
# ['D', 'D', 'S', 'S']]函数create_samples(n_sons, n_daughters)返回符合条件的所有示例,假设n_sons和n_daughters仍待处理。
https://stackoverflow.com/questions/64435035
复制相似问题