2 つの変数 (辞書とリスト) の値を比較したい。辞書にはネストされた構造があるため、すべての項目をループする必要があります。簡単な解決策を発見しましたが、これをより良い方法で(Pythonを使用して)実行できると確信しています。簡単に言えば、変数user_from_database
に存在しないアイテムを見つけたいと思います。user_from_client
私の解決策:
#variable containing users from client side
users_from_client = {
"0": {
"COL1": "whatever",
"COL2": "val1",
"COL3": "whatever",
},
"1": {
"COL1": "whatever",
"COL2": "val2",
"COL3": "whatever",
},
"3": {
"COL1": "whatever",
"COL2": "val3",
"COL3": "whatever",
}
}
#variable containing users from the database
users_from_database = [
["val1"],
["val2"],
["val5"],
["val7"]
]
#This function is used to find element from the nested dictionaries(d)
def _check(element, d, pattern = 'COL2'):
exist = False
for k, user in d.iteritems():
for key, item in user.iteritems():
if key == pattern and item == element:
exist = True
return exist
#Finding which users should be removed from the database
to_remove = []
for user in users_from_db:
if not _check(user[0], users_from_agent):
if user[0] not in to_remove:
to_remove.append(user[0])
#to_remove list contains: [val5, val7"]
Pythonアプローチを使用して同じ結果を得るより良い方法は何ですか? おそらく、私が Python の初心者であることを付け加える必要はありません (上記のコードを見ればわかると思います)。