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

2024-04-20 07:10:34 发布

您现在位置:Python中文网/ 问答频道 /正文

参考文件:文件.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


Tags: 文件py字典typegroupmsgoperationclass
3条回答

在这里,使用dict的dicts,这样的东西看起来更合适:

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

更新

在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会理解这一点。基本上,view将项作为类似集合的对象返回。我得到两个集合的交集,并检查以确保它是一个完整的交集。换句话说,我检查以确保一个字典是另一个字典的子集。我不能保证这比我的另一个答案更有效,但它更简洁一点(如果它更快,我也不会感到惊讶)。我想我们需要timeit才能知道。你知道吗


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

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))

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

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)]

相关问题 更多 >