![](/img/trans.png)
[英]Mapping Python dictionary with multiple keys into dataframe with multiple columns matching keys
[英]Matching multiple dictionary keys at a large scale in python
所以,我的程序有一個值變化很大的字典。
example_dict = {'a': True, 'b' : "b", 'c' : "c", 'd' : "d", 'e' : False, 'f' : {'fa' : "", 'fb' : "b", 'fc' : "fc", "fd" : 0, "fe" : 0, "ff" : 0, "fg" : []}}
我有一些 if 條件需要檢查其中一些鍵(不是全部)是否匹配。
如果我需要檢查幾個條件,那么使用“和”的方法是有效的。
if example_dict['a'] == True and example_dict['b'] == "b" and example_dict['c'] == "c" or example_dict['a'] == False and example_dict['b'] == "bb" and example_dict['c'] == "c":
上面的示例將檢查以下內容,以查看它們是否存在於示例字典中:
{'a':True, 'b':"b", 'c':"c"}
{'a':False, 'b':"bb", 'c':"c"}
問題是有時我需要檢查 10 多組鍵值。 似乎應該有一種更簡單的方法來做到這一點,但我不知所措。 任何幫助,將不勝感激。
這是一個可能的解決方案:
def dict_agree(req, d):
return all([v == d[k] for k, v in req.items()])
# Demonstration
example_dict = {'a': True, 'b' : "b", 'c' : "c", 'd' : "d", 'e' : False,
'f' : {'fa' : "", 'fb' : "b", 'fc' : "fc", "fd" : 0, "fe" : 0, "ff" : 0, "fg" : []}}
requirement_dict_1 = {'a': False, 'b': 'b', 'c': 'c'}
requirement_dict_2 = {'a': True, 'b': 'b', 'c': 'c'}
print(dict_agree(requirement_dict_1, example_dict)) # False, because 'a' has the wrong value
print(dict_agree(requirement_dict_1, example_dict)) # True, all the requirements are satisfied
all
迭代器在這里很有用。 將您的測試條件放在一個數組中,然后使用列表理解來獲得結果。
conditions = [('a', True), ('b', 'B'), ('c', 3)]
example_dict = {'a': True, 'b': 'B', 'c': 3}
match = all(example_dict[k] == v for k, v in conditions)
編輯:添加多個條件集
要檢查條件集,請輸入any
。
# Declare sets of conditions to test
conditions = [[('a', True), ('b', 'B')],
[('a', True), ('c', 3)],
[('a', True), ('c', 34)]]
# An example dictionary to test
example_dict = {'a': True, 'b': 'B', 'c': 3}
# Test if any of the condition sets match. The any iterator will
# return True at the first matching set of conditions. The all
# iterator will compare each key and value within a set of conditions.
match = any(all(example_dict[k] == v for k, v in c) for c in conditions)
你可以使用:
cond = {(True, 'b', 'c'), (False, 'bb', 'c')}
tuple(map(example_dict.get, ('a', 'b', 'c'))) in cond
這將是 O(M) 時間復雜度,其中 M 是您要檢查的鍵數
好的,我正在回答我自己的問題,因為我正在實現兩者的混合,以防有人稍后閱讀。 這是似乎對我有用的代碼:
def main():
# Dictionary to check
example_dict = {'a': True, 'b' : "b", 'c' : "c", 'd' : "d", 'e' : False, 'f' : {'fa' : "", 'fb' : "b", 'fc' : "fc", "fd" : 0, "fe" : 0, "ff" : 0, "fg" : []}}
# Conditions to find within the dictionary
conditions = [[('a', True), ('b', "b"), ('c', "c")], [('a', False), ('b', "bb"), ('c', "c")]]
# If the conditions aren't met, print false, else true
if not check_conditions(example_dict, conditions):
print("FALSE: a: {} b: {} c: {}".format(example_dict['a'], example_dict['b'], example_dict['c']))
else:
print("TRUE: a: {} b: {} c: {}".format(example_dict['a'], example_dict['b'], example_dict['c']))
# broken up into a function because my program
# is broken up over multiple files
def check_conditions(my_dict, condition_list):
return any(all(my_dict[k] == v for k, v in c) for c in condition_list)
按原樣使用字典輸出:
真:a:真 b:bc:c
輸出,當 'b' 替換為 'bb' 時(第一個不滿足 'b' 條件,第二個不滿足 'a' 條件):
錯誤:a:正確 b:bb c:c
該代碼在字典上使用元組。 我猜測為什么這有效而另一種方式無效的原因是因為我認為代碼正在檢查整個字典是否匹配,而元組正在隔離特定的鍵。
謝謝你們每一個人的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.