Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
确定哪个词典与python中的Dictionary类匹配_Python_Python 2.7 - Fatal编程技术网

确定哪个词典与python中的Dictionary类匹配

确定哪个词典与python中的Dictionary类匹配,python,python-2.7,Python,Python 2.7,referenece文件:file.py class requestDicts(): dictrefA={ "operation_module":"cbs", "operation_group":"xxx", "operation_type":"yyy"} dictrefB={ "operation_module":"cbs", "operation_group":"xxx", "operation_type":"yyy1"}

referenece文件:file.py

class requestDicts():
    dictrefA={
    "operation_module":"cbs",
    "operation_group":"xxx",
    "operation_type":"yyy"}

    dictrefB={
    "operation_module":"cbs",
    "operation_group":"xxx",
    "operation_type":"yyy1"}

    dictrefC={
    "operation_module":"cbs",
    "operation_group":"xxx1",
    "operation_type":"yyy1"}
比较文件头.py

recievedDict={
    "msg_id":100,
    "operation_module":"cbs",
    "operation_group":"xxx",
    "operation_type":"yyy1",
    "user_name":"venkat",
    "msg_length":50}
如何对字典执行部分比较并返回其匹配的字典的名称


上面的预期答案是:
dictrefB
更新

在python2.7和python3.x上,有一种更好的方法:

items = ((k,v) for (k,v) in requestDicts.__dict__.items() if k.startswith('dictref'))
r_items = recievedDict.viewitems()
print next(k for k,sub in items if len(sub.viewitems() & r_items) == len(sub))
第一行只是尝试将包含字典的类放入稍微有用的数据结构中。第二行只是缩短第三行,避免在生成器中进行额外的属性查找/方法调用

在python3.x上,
dict.items
返回的内容与python2.x中的
dict.viewitems
返回的内容相同。我想
2to3
会理解这一点。基本上,
视图
将项目作为类似集合的对象返回。我得到两个集合的交集,并检查以确保它是一个完整的交集。换句话说,我检查以确保一个字典是另一个字典的子集。我不能保证这比我的另一个答案更有效,但它更简洁(如果它更快,我也不会感到惊讶)。我想我们需要知道时间


我认为没有比循环更好的方法来比较它们:

def compare(sub,full):
    try:
        return all(sub[k] == full[k] for k in sub)
    except KeyError:
        return False  #sub's keys aren't a subset of full's keys.
现在要确定哪一个是第一个匹配项:

next(sub for sub in (dictrefA,dictrefB,dictrefC) if compare(sub,recievedDict))
下面是一个完整、具体的工作示例:

class requestDicts():
    dictrefA={
    "operation_module":"cbs",
    "operation_group":"xxx",
    "operation_type":"yyy"}

    dictrefB={
    "operation_module":"cbs",
    "operation_group":"xxx",
    "operation_type":"yyy1"}

    dictrefC={
    "operation_module":"cbs",
    "operation_group":"xxx1",
    "operation_type":"yyy1"}

recievedDict={
    "msg_id":100,
    "operation_module":"cbs",
    "operation_group":"xxx",
    "operation_type":"yyy1",
    "user_name":"venkat",
    "msg_length":50}


def compare(sub,full):
    try:
        return all(sub[k] == full[k] for k in sub)
    except KeyError:
        return False  #sub's keys aren't a subset of full's keys.

items = ((k,v) for (k,v) in requestDicts.__dict__.items() if k.startswith('dictref'))
print next(k for k,sub in items if compare(sub,recievedDict))

更新

在python2.7和python3.x上,有一种更好的方法:

items = ((k,v) for (k,v) in requestDicts.__dict__.items() if k.startswith('dictref'))
r_items = recievedDict.viewitems()
print next(k for k,sub in items if len(sub.viewitems() & r_items) == len(sub))
第一行只是尝试将包含字典的类放入稍微有用的数据结构中。第二行只是缩短第三行,避免在生成器中进行额外的属性查找/方法调用

在python3.x上,
dict.items
返回的内容与python2.x中的
dict.viewitems
返回的内容相同。我想
2to3
会理解这一点。基本上,
视图
将项目作为类似集合的对象返回。我得到两个集合的交集,并检查以确保它是一个完整的交集。换句话说,我检查以确保一个字典是另一个字典的子集。我不能保证这比我的另一个答案更有效,但它更简洁(如果它更快,我也不会感到惊讶)。我想我们需要知道时间


我认为没有比循环更好的方法来比较它们:

def compare(sub,full):
    try:
        return all(sub[k] == full[k] for k in sub)
    except KeyError:
        return False  #sub's keys aren't a subset of full's keys.
现在要确定哪一个是第一个匹配项:

next(sub for sub in (dictrefA,dictrefB,dictrefC) if compare(sub,recievedDict))
下面是一个完整、具体的工作示例:

class requestDicts():
    dictrefA={
    "operation_module":"cbs",
    "operation_group":"xxx",
    "operation_type":"yyy"}

    dictrefB={
    "operation_module":"cbs",
    "operation_group":"xxx",
    "operation_type":"yyy1"}

    dictrefC={
    "operation_module":"cbs",
    "operation_group":"xxx1",
    "operation_type":"yyy1"}

recievedDict={
    "msg_id":100,
    "operation_module":"cbs",
    "operation_group":"xxx",
    "operation_type":"yyy1",
    "user_name":"venkat",
    "msg_length":50}


def compare(sub,full):
    try:
        return all(sub[k] == full[k] for k in sub)
    except KeyError:
        return False  #sub's keys aren't a subset of full's keys.

items = ((k,v) for (k,v) in requestDicts.__dict__.items() if k.startswith('dictref'))
print next(k for k,sub in items if compare(sub,recievedDict))

类似的内容在这里看起来更合适,使用dict的
dict

main_dict={
    'dictrefA':{
      "operation_module":"cbs",
      "operation_group":"xxx",
      "operation_type":"yyy"},
    'dictrefB':{
      "operation_module":"cbs",
      "operation_group":"xxx",
      "operation_type":"yyy1"},
    'dictrefC':{
      "operation_module":"cbs",
      "operation_group":"xxx1",
      "operation_type":"yyy1"}
      }

recievedDict={
    "msg_id":100,
    "operation_module":"cbs",
    "operation_group":"xxx",
    "operation_type":"yyy1",
    "user_name":"venkat",
    "msg_length":50}

for x,y in main_dict.items():
    if all(v in recievedDict.items() for v in y.items()):
        print x
        break
输出

dictrefB

类似的内容在这里看起来更合适,使用dict的
dict

main_dict={
    'dictrefA':{
      "operation_module":"cbs",
      "operation_group":"xxx",
      "operation_type":"yyy"},
    'dictrefB':{
      "operation_module":"cbs",
      "operation_group":"xxx",
      "operation_type":"yyy1"},
    'dictrefC':{
      "operation_module":"cbs",
      "operation_group":"xxx1",
      "operation_type":"yyy1"}
      }

recievedDict={
    "msg_id":100,
    "operation_module":"cbs",
    "operation_group":"xxx",
    "operation_type":"yyy1",
    "user_name":"venkat",
    "msg_length":50}

for x,y in main_dict.items():
    if all(v in recievedDict.items() for v in y.items()):
        print x
        break
输出

dictrefB
结果:

$ python so.py
dictrefB
如果需要更快,您可以制作一个哈希字典

结果:

$ python so.py
dictrefB

如果需要更快,您可以制作一个散列字典。

假设您的原始数据,我将创建一个中间查找和一个帮助函数,然后执行以下操作:

from operator import itemgetter

key = itemgetter('operation_module', 'operation_group', 'operation_type')
lookup = {key(v): k for k,v in requestDicts.__dict__.iteritems() if isinstance(v, dict)}
print lookup[key(recievedDict)]

假设您的原始数据,我将创建一个中间查找和一个助手函数,并执行以下操作:

from operator import itemgetter

key = itemgetter('operation_module', 'operation_group', 'operation_type')
lookup = {key(v): k for k,v in requestDicts.__dict__.iteritems() if isinstance(v, dict)}
print lookup[key(recievedDict)]

变量名只是对对象的引用。最好在这里创建一个dict,或者甚至是一个dict列表……变量名只是一个对象的引用。最好在这里创建一个dict。或者甚至可能是一个dict列表…@mgilson是的-想象一个变化:)我认为
viewitems
(或者仅仅是py3k上的
items
)是最好的方法。这有点令人沮丧,因为
视图
不是实际的
,所以我不能使用
issubset
,但这没关系——长度测试也很有效。@mgilson是的-想象了一个变化:)我认为
视图项
(或者只是py3k上的
)是最好的方法。这有点令人沮丧,因为
视图
不是实际的
集合
,所以我不能使用
issubset
,但这没关系——长度测试也可以。