比较两个字典并显示它们之间的差异作为结果。

huangapple go评论74阅读模式
英文:

python compare 2 dictionary and show difference in both as result

问题

def get_difference(source_dict=None, db_dict=None):
    NONE = None
    algo_list_db = set(source_dict.keys())
    algo_list_sd = set(db_dict.keys())
    get_all_keys_sd_db = list(algo_list_db.union(algo_list_db, algo_list_sd))
    result_enabled = {}
    result_disabled = {}
    for algo_name in get_all_keys_sd_db:
        is_key_present_db = algo_name in source_dict
        is_key_present_sd = algo_name in db_dict
        if is_key_present_db == is_key_present_sd:
            car_diff_enabled = set(source_dict.get(algo_name, NONE)) - set(db_dict.get(algo_name, NONE))
            car_diff_disabled = set(db_dict.get(algo_name, NONE)) - set(source_dict.get(algo_name, NONE))
            result_enabled[algo_name] = '' if not car_diff_disabled else list(car_diff_disabled)
            result_disabled[algo_name] = '' if not car_diff_enabled else list(car_diff_enabled)
        elif is_key_present_sd == 1 and is_key_present_db == 0:
            car_diff_enabled = list(db_dict.get(algo_name, NONE))
            result_enabled[algo_name] = car_diff_enabled
        elif is_key_present_sd == 0 and is_key_present_db == 1:
            car_diff_enabled = list(source_dict.get(algo_name, NONE))
            result_enabled[algo_name] = car_diff_enabled
    return str(result_enabled), str(result_disabled)

before_data = {
    'electric': ['benz1', 'benz2'], 'petrol': ['bmw1', 'bmw2', 'bmw3'], '': ['jaggur1', 'jaggur2', 'jaggur3']
}
after_data = {
    'electric': ['benz1'], 'petrol': ['bmw1', 'bmw2', 'bmw3', 'benz4'], '': ['jaggur4'], 'gas': ['ferrari1']
}
# Output that I expect:
# Data that has been added
enabled_data {'': ['jaggur4'], 'gas': ['ferrari1'], 'petrol': ['benz4'], 'electric': ' '}
disabled_data {'': ['jaggur2', 'jaggur1', 'jaggur3'], 'petrol': ' ', 'electric': ['benz2']}
英文:

I have two dictionaries that I need to compare. The difference should be found between A - B and B - A. The result should be shown as below...

I have written the code but it looks too complex. I wonder how can I refactor the code to look simple and elegant.

Python

def get_sd_db_stepids_difference(source_dict=None, db_dict=None):
    NONE = None
    algo_list_db = set(source_dict.keys())
    algo_list_sd = set(db_dict.keys())
    get_all_keys_sd_db=list(algo_list_db.union(algo_list_db,algo_list_sd))
    result_enabled={}
    result_disabled = {}
    for algo_name in get_all_keys_sd_db:
        is_key_present_db = algo_name in source_dict
        is_Key_Present_sd = algo_name in db_dict
        if  is_key_present_db == is_Key_Present_sd:
            car_diff_enabled = set(source_dict.get(algo_name, NONE)) - set(db_dict.get(algo_name, NONE))
            car_diff_disabled = set(db_dict.get(algo_name, NONE)) - set(source_dict.get(algo_name, NONE))
            result_enabled[algo_name] = ' ' if len(car_diff_disabled) == 0 else list(car_diff_disabled)
            result_disabled[algo_name] = ' ' if len(car_diff_enabled) == 0 else list(car_diff_enabled)
        elif is_Key_Present_sd==1 and  is_key_present_db==0:
            car_diff_enabled = list(db_dict.get(algo_name, NONE))
            result_enabled[algo_name] = car_diff_enabled
        elif is_Key_Present_sd == 0 and is_key_present_db == 1:
            car_diff_enabled = list(source_dict.get(algo_name, NONE))
            result_enabled[algo_name] = car_diff_enabled
    return  str(result_enabled),str(result_disabled)

before_data= {
    'electric': ['benz1', 'benz2'], 'petrol': ['bmw1', 'bmw2', 'bmw3'], '': ['jaggur1', 'jaggur2','jaggur3']
}
after_data= {
    'electric': ['benz1'], 'petrol': ['bmw1','bmw2','bmw3','benz4'], '': ['jaggur4'], 'gas':['ferrai1']
}

# Output that I expect: 
#
# Data that has been added
enabled_data {'': ['jaggur4'], 'gas': ['ferrai1'], 'petrol': ['benz4'], 'electric': ' '}
disabled_data {'': ['jaggur2', 'jaggur1', 'jaggur3'], 'petrol': ' ', 'electric': ['benz2']}

Highly appreciate your inputs and suggestions!

Just in case, I'm using Python 2.7.

答案1

得分: 0

一旦您升级到Python 3.8+(确实应该这样做),然后...

before_data = {
    'electric': ['benz1', 'benz2'],
    'petrol': ['bmw1', 'bmw2', 'bmw3'],
    '': ['jaggur1', 'jaggur2', 'jaggur3']
}

after_data = {
    'electric': ['benz1'],
    'petrol': ['bmw1', 'bmw2', 'bmw3', 'benz4'],
    '': ['jaggur4'],
    'gas': ['ferrai1']
}

def compare(a, b):
    result = {}
    for k, v in a.items():
        if (_v := b.get(k)) is not None:
            if diff := set(v) - set(_v):
                result[k] = list(diff)
            else:
                result[k] = ' '
        else:
            result[k] = v
    return result

print(compare(before_data, after_data))
print(compare(after_data, before_data))

输出:

{'electric': ['benz2'], 'petrol': ' ', '': ['jaggur1', 'jaggur2', 'jaggur3']}
{'electric': ' ', 'petrol': ['benz4'], '': ['jaggur4'], 'gas': ['ferrai1']}
英文:

Once you've updated to Python 3.8+ (which you really should do) then...

before_data= {
    'electric': ['benz1', 'benz2'],
    'petrol': ['bmw1', 'bmw2', 'bmw3'],
    '': ['jaggur1', 'jaggur2','jaggur3']
}
after_data= {
    'electric': ['benz1'],
    'petrol': ['bmw1','bmw2','bmw3','benz4'],
    '': ['jaggur4'],
    'gas':['ferrai1']
}

def compare(a, b):
    result = {}
    for k, v in a.items():
        if (_v := b.get(k)) is not None:
            if diff := set(v) - set(_v):
                result[k] = list(diff)
            else:
                result[k] = ' '
        else:
            result[k] = v
    return result

print(compare(before_data, after_data))
print(compare(after_data, before_data))

Output:

{'electric': ['benz2'], 'petrol': ' ', '': ['jaggur1', 'jaggur2', 'jaggur3']}
{'electric': ' ', 'petrol': ['benz4'], '': ['jaggur4'], 'gas': ['ferrai1']}

huangapple
  • 本文由 发表于 2023年5月15日 01:21:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定