在python中已经有了一个multi key dict和一个多值dict。我需要一个python字典,它既有:
示例:
# probabilistically fetch any one of baloon, toy or car
d['red','blue','green']== "baloon" or "car" or "toy"
d['red']==d['green']的概率很高,d['red']的概率!=d['red']较低,但有可能
单个输出值应根据键的规则进行概率确定(模糊) 例:在上面的情况下,规则可能是如果钥匙同时有“红色”和“蓝色”,则返回“baloon”80%的时间,如果只有蓝色,则返回“玩具”15%的时间,否则“汽车”5%的时间。在
setitem方法的设计应使以下内容成为可能:
^{pr2}$上面使用谓词函数和相应的概率将多个值赋给字典。而不是上面的作业列表,甚至是字典作为作业更可取:
d["red", "blue"] ={
"baloon": haseither('red','green',0.8),
"toy": hasonly("blue",0.15),
"car": default(0.05)
}
在上述情况下,如果出现“红色”或“绿色”,将返回80%的时间 ,如有蓝色,则退还玩具15%的时间,无条件退还汽车5%的时间。在
在python中有没有已经满足上述要求的现有数据结构?如果没有,那么如何在python中修改multikeydict代码以满足上述要求?在
如果使用dictionary,那么可以有一个配置文件或使用适当的嵌套修饰符来配置上述概率谓词逻辑,而不必硬编码if\else语句。在
注意:以上是基于规则的自动响应程序应用程序的一个有用的自动机,因此请告诉我python中是否有类似的基于规则的框架,即使它不使用字典结构?在
模拟多键词典
^{} 不允许^{} 同时具有多个密钥。。。在
(例如
d["red", "green"]
)多键可以用^{} 或^{} 键来模拟。如果顺序无关紧要,^{} 似乎是最好的(实际上是散列的^{} ,因此[
"red", "blue"]
与["blue", "red"]
相同。在模拟多元词典
多值是使用某些数据类型固有的,它可以是any storage element,可以方便地编制索引。标准^{} 应该提供这一点。在
非决定论
使用由规则和假设定义的概率分布1,使用python文档中的this recipe执行非确定性选择。在
MultiKeyMultiValNonDeterministicDict
类多好的名字啊。\o/-不错!
此类接受多个键,这些键定义了一个由多个值组成的概率规则集。在项目创建(^{} )期间,所有键组合的值概率都会预先计算1。在项目访问(^{} )期间,选择预先计算的概率分布,并基于随机加权选择对结果进行评估。在
定义
使用
^{pr2}$测试
检查概率
概率符合规则!
脚注
分布假设
由于规则集没有完全定义,所以对概率分布进行了假设,大部分假设都是在^{中完成的。基本上,任何未定义的概率都将均匀地分布在剩余值上。这是针对所有键组合进行的,并为随机加权选择创建一个通用键接口。在
给出示例规则集
这将创建以下发行版
如果这是不正确的,请告知。
如果可以更改数据结构,那么使用返回所需数据的函数会更简单。这将是完全灵活的,可以容纳任何类型的数据,如果您以后需要更改它们。在
输出(第二行明显改变):
^{pr2}$请记住,您的案例分析并不完整,而且很模糊,但您可以“在精神上”做以下事情(充实所需的结果):
我会把它作为一个函数,因为它是一个函数!但是如果您坚持让它像dict一样,那么python让您通过重写
^{pr2}$__getitem__
(在我看来它不是python)来实现这一点。在并希望通过OP获得澄清:
要生成如下函数:
在这种情况下,临界值和默认值是:
正如我所说,你的概率是模棱两可的/不加总的,所以你很可能需要调整一下。。。
可以通过在实例化时传递crit和default来扩展类定义:
相关问题 更多 >
编程相关推荐