0

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

4

4 回答 4

2

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

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)]
于 2013-04-02T15:19:51.787 回答
1

更新

在 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返回与dict.viewitems在 python2.x 中相同的内容。我想那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))
于 2013-04-02T15:05:26.877 回答
1

像这样的东西在这里看起来更合适,使用一个dictdicts:

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
于 2013-04-02T15:09:04.013 回答
0
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"}
      }
recieved_dict={
    "msg_id":100,
    "operation_module":"cbs",
    "operation_group":"xxx",
    "operation_type":"yyy1",
    "user_name":"venkat",
    "msg_length":50}

compare_dict = {
    "operation_module": recieved_dict["operation_module"],
    "operation_group": recieved_dict["operation_group"],
    "operation_type": recieved_dict["operation_type"],
}

matching_dict_key = [key for key, value in main_dict.items() if value == compare_dict]

print matching_dict_key[0]

结果:

$ python so.py
dictrefB

如果它需要更快,你可以制作一个哈希字典。

于 2013-04-02T15:13:27.330 回答