打印序列和按值排序的值
Print sequence and value ordered by the value
我有一个代码,它包含 6 到 10 张红色或黑色卡片序列的所有可能组合。借助数学方程式,根据两个抽牌序列计算给定的 surprise
指标。这是代码:
from itertools import combinations_with_replacement
deckOrder=[]
box_1=['R','B']
comb = combinations_with_replacement(box_1, 6)
for i in list(comb):
deckOrder.append(i)
comb = combinations_with_replacement(box_1, 7)
for i in list(comb):
deckOrder.append(i)
comb = combinations_with_replacement(box_1, 8)
for i in list(comb):
deckOrder.append(i)
comb = combinations_with_replacement(box_1, 9)
for i in list(comb):
deckOrder.append(i)
comb = combinations_with_replacement(box_1, 10)
for i in list(comb):
deckOrder.append(i)
outcome = []
for i in range(len(deckOrder)):
for j in range(len(deckOrder)):
#Draw
first_draw = deckOrder[i]
second_draw = deckOrder[j]
#Before first draw
initial_estimate = 0.5
initial_variance = 1/12
#After first draw
r_count = first_draw.count('R')
b_count = first_draw.count('B')
alpha = 1 + r_count
beta = 1 + b_count
e_of_theta = alpha/(alpha+beta)
surprise = ((e_of_theta - initial_estimate)**2)/(initial_variance)
var_theta = (alpha * beta)/ ((alpha + beta) **2 *(alpha + beta + 1))
#After second draw
r_count = second_draw.count('R')
b_count = second_draw.count('B')
new_alpha = alpha + r_count
new_beta = beta + b_count
new_e_of_theta = new_alpha/(new_alpha + new_beta)
surprise = ((new_e_of_theta - e_of_theta)**2)/var_theta
#Export
new_list = [surprise]
for a in new_list:
outcome.append(a)
order = sorted(outcome)
print(*order,sep='\n')
代码现在只显示 suprise
值。有什么方法可以同时显示使用的序列和值,这样我就可以知道哪个值以有序的方式属于哪个序列(通过 surprise
val)?
您可以使用 OrderedDict
来获得有序的数据结构并使其成为
可以将每个序列映射到一个值
如前一个答案所建议,解决此问题的一个很好的候选者是使用 OrderedDict
,其中 surprise
用作键,序列存储为值。原始片段虽然非常杂乱无章,但缩进有缺陷。这是我的解决方案建议:
from itertools import combinations_with_replacement
from collections import OrderedDict
deckOrder, card_colors, draw_num_cards = [], ['R', 'B'], [x for x in range(6, 11)]
for num_cards in draw_num_cards:
combs = combinations_with_replacement(card_colors, num_cards)
deckOrder.extend(list(combs))
outcome = {}
initial_estimate = 0.5
initial_variance = 1/12
for i in range(len(deckOrder)):
#Draw
first_draw = deckOrder[i]
#After first draw
r_count = first_draw.count('R')
b_count = first_draw.count('B')
#Before first draw
alpha = 1 + r_count
beta = 1 + b_count
for j in range(len(deckOrder)):
#Draw
second_draw = deckOrder[j]
e_of_theta = alpha/(alpha+beta)
surprise = ((e_of_theta - initial_estimate)**2) / (initial_variance)
var_theta = (alpha * beta) / ((alpha + beta)**2 * (alpha + beta + 1))
#After second draw
r_count = second_draw.count('R')
b_count = second_draw.count('B')
new_alpha = alpha + r_count
new_beta = beta + b_count
new_e_of_theta = new_alpha / (new_alpha + new_beta)
surprise = ((new_e_of_theta - e_of_theta)**2) / var_theta
sequences = (first_draw, second_draw)
outcome[surprise] = sequences
#sort by `surprise`
outcome_ordered = OrderedDict()
for surprise in sorted(outcome.keys()):
outcome_ordered[surprise] = outcome[surprise]
for surprise, (first, second) in outcome_ordered.items():
print(surprise, ": ", first, second)
我有一个代码,它包含 6 到 10 张红色或黑色卡片序列的所有可能组合。借助数学方程式,根据两个抽牌序列计算给定的 surprise
指标。这是代码:
from itertools import combinations_with_replacement
deckOrder=[]
box_1=['R','B']
comb = combinations_with_replacement(box_1, 6)
for i in list(comb):
deckOrder.append(i)
comb = combinations_with_replacement(box_1, 7)
for i in list(comb):
deckOrder.append(i)
comb = combinations_with_replacement(box_1, 8)
for i in list(comb):
deckOrder.append(i)
comb = combinations_with_replacement(box_1, 9)
for i in list(comb):
deckOrder.append(i)
comb = combinations_with_replacement(box_1, 10)
for i in list(comb):
deckOrder.append(i)
outcome = []
for i in range(len(deckOrder)):
for j in range(len(deckOrder)):
#Draw
first_draw = deckOrder[i]
second_draw = deckOrder[j]
#Before first draw
initial_estimate = 0.5
initial_variance = 1/12
#After first draw
r_count = first_draw.count('R')
b_count = first_draw.count('B')
alpha = 1 + r_count
beta = 1 + b_count
e_of_theta = alpha/(alpha+beta)
surprise = ((e_of_theta - initial_estimate)**2)/(initial_variance)
var_theta = (alpha * beta)/ ((alpha + beta) **2 *(alpha + beta + 1))
#After second draw
r_count = second_draw.count('R')
b_count = second_draw.count('B')
new_alpha = alpha + r_count
new_beta = beta + b_count
new_e_of_theta = new_alpha/(new_alpha + new_beta)
surprise = ((new_e_of_theta - e_of_theta)**2)/var_theta
#Export
new_list = [surprise]
for a in new_list:
outcome.append(a)
order = sorted(outcome)
print(*order,sep='\n')
代码现在只显示 suprise
值。有什么方法可以同时显示使用的序列和值,这样我就可以知道哪个值以有序的方式属于哪个序列(通过 surprise
val)?
您可以使用 OrderedDict
来获得有序的数据结构并使其成为
可以将每个序列映射到一个值
如前一个答案所建议,解决此问题的一个很好的候选者是使用 OrderedDict
,其中 surprise
用作键,序列存储为值。原始片段虽然非常杂乱无章,但缩进有缺陷。这是我的解决方案建议:
from itertools import combinations_with_replacement
from collections import OrderedDict
deckOrder, card_colors, draw_num_cards = [], ['R', 'B'], [x for x in range(6, 11)]
for num_cards in draw_num_cards:
combs = combinations_with_replacement(card_colors, num_cards)
deckOrder.extend(list(combs))
outcome = {}
initial_estimate = 0.5
initial_variance = 1/12
for i in range(len(deckOrder)):
#Draw
first_draw = deckOrder[i]
#After first draw
r_count = first_draw.count('R')
b_count = first_draw.count('B')
#Before first draw
alpha = 1 + r_count
beta = 1 + b_count
for j in range(len(deckOrder)):
#Draw
second_draw = deckOrder[j]
e_of_theta = alpha/(alpha+beta)
surprise = ((e_of_theta - initial_estimate)**2) / (initial_variance)
var_theta = (alpha * beta) / ((alpha + beta)**2 * (alpha + beta + 1))
#After second draw
r_count = second_draw.count('R')
b_count = second_draw.count('B')
new_alpha = alpha + r_count
new_beta = beta + b_count
new_e_of_theta = new_alpha / (new_alpha + new_beta)
surprise = ((new_e_of_theta - e_of_theta)**2) / var_theta
sequences = (first_draw, second_draw)
outcome[surprise] = sequences
#sort by `surprise`
outcome_ordered = OrderedDict()
for surprise in sorted(outcome.keys()):
outcome_ordered[surprise] = outcome[surprise]
for surprise, (first, second) in outcome_ordered.items():
print(surprise, ": ", first, second)