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"}

比較ファイル head.py

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

辞書の部分比較を実行し、一致する辞書の名前を返す方法

上記の予想される答えは次のとおりです。dictrefB

4

4 に答える 4

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

最初の行は、クラス保持辞書をもう少し便利なデータ構造にしようとする試みです。2 行目は単に 3 行目を短くし、ジェネレーターでの余分な属性検索/メソッド呼び出しを避けるためのものです。

python3.x では、python2.x で行ったdict.itemsのと同じことを返します。dict.viewitemsそれはそれで2to3わかると思います。基本的に、viewはアイテムをセットのようなオブジェクトとして返します。2 つのセットの交差を取得し、それが完全な交差であることを確認します。つまり、一方の辞書が他方の辞書のサブセットであることを確認します。これが私の他の回答よりも効率的であることを保証するものではありませんが、もう少し簡潔です (より高速であっても驚かないでしょう)。私たちは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

ここでは、辞書の a を使用して、このようなものがより適切に見え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
于 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 に答える