使用Python提取存储在2D字典数组中的值

时间:2017-12-05 14:27:40

标签: python arrays numpy dictionary for-loop

我编写了一段代码来确定在抛出X骰子时获得值Y的概率。我使用的等式包含范围0的总和,包括Y.

我想在网格上绘制值(骰子的数量 - 值的总和)并创建一个网格。

import scipy.special
from matplotlib import pyplot as plt
from matplotlib import cm as cm
import numpy as np
from numpy import exp,arange

def probability_of_sum(sum, dice, faces):
    # Give different names.
    X = sum
    Y = dice
    Z = faces

    # Calculations, calculations, calculations.
    # Dummy values.
    wanted_possibilities = 1
    total_possibilities = 2
    probability = wanted_possibilities/total_possibilities

    return {'wanted_possibilities':wanted_possibilities, 'total_possibilities':total_possibilities, 'probability':probability}


# Main part of the function.
# Consider 1 to 8 dice
# Get probabilities for lowest possible sum (8) to highest possible sum (48)

dice_range = np.linspace(1, 8, num=8)
value_range = np.linspace(1, 8*6, num=8*6)
X,Y = np.meshgrid( dice_range, value_range)

# Calculate value for each grid point separately using VECTORIZE.
value_prob = np.vectorize(probability_of_sum)( Y, X, 6)

我的函数的输出现在是一个2D字典数组(下面是虚拟值):

[[{'Wanted_possibilities': 0, 'total_possibilities':1, 'probability':0}
{'Wanted_possibilities': 1, 'total_possibilities':1, 'probability':1}]
 [{'Wanted_possibilities': 0, 'total_possibilities':2, 'probability':0}
{'Wanted_possibilities': 1, 'total_possibilities':2, 'probability':0.5}]
 [{'Wanted_possibilities': 0, 'total_possibilities':3, 'probability':0}
{'Wanted_possibilities': 1, 'total_possibilities':3, 'probability':0.33}]
 [{'Wanted_possibilities': 0, 'total_possibilities':4, 'probability':0}
{'Wanted_possibilities': 1, 'total_possibilities':4, 'probability':0.25}] ]

如何从这里继续绘制输出('概率')作为X和Y的2D函数?

value_prob['probability']

给我错误

  

IndexError:只有整数,切片(:),省略号(...),numpy.newaxis(None)和整数或布尔数组才是有效索引。

我可以使用

访问各个元素
(value_prob[1,1])['probability']

然后我需要一个循环来绘制函数。是否有更通用/更强大/更清晰的方式来访问它?

编辑:我想用'骰子,价值'来描绘'概率'。

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

import matplotlib.pyplot as plt
import itertools
import numpy as np
s = [[{'Wanted_possibilities': 0, 'total_possibilities':1, 'probability':0},
{'Wanted_possibilities': 1, 'total_possibilities':1, 'probability':1}],
 [{'Wanted_possibilities': 0, 'total_possibilities':2, 'probability':0},
{'Wanted_possibilities': 1, 'total_possibilities':2, 'probability':0.5}],
 [{'Wanted_possibilities': 0, 'total_possibilities':3, 'probability':0},
{'Wanted_possibilities': 1, 'total_possibilities':3, 'probability':0.33}],
 [{'Wanted_possibilities': 0, 'total_possibilities':4, 'probability':0},
{'Wanted_possibilities': 1, 'total_possibilities':4, 'probability':0.25}] ]
final_data = list(itertools.chain(*[[[i['Wanted_possibilities'], i['total_possibilities'], i['probability']] for i in b] for b in s]))
plt.bar(np.arange(len([i[-1] for i in final_data])), [i[0] for i in final_data], 0.60, yerr = [i[1] for i in final_data])
plt.ylabel('Probabilities')
plt.xticks(np.arange(len([i[-1] for i in final_data])), tuple(map(str, [i[-1] for i in final_data])))
plt.show()

enter image description here