如何比较3个字典并返回键和值



我想比较3个字典,并找出所有3个字典中的变量及其相关值都是相同的,哪些变量及其值都不同。

通过读取3个文本文件,可以找到3个字典的键和值

说我有3个字典如下:

d1 = {'var1': ' ', 'var2': 'high', 'var3': '50'}
d2 = {'var2': 'low', 'var3': '50', 'var4': '80'}
d3 = {'var2': 'high', 'var3': '50', 'var4': '100'}

我的最终结果是将其保存到文本文件中,然后在Excel中将其打开,其中结果将在列中显示,然后我应该看到以下内容:

Common Variables
var3 50 50 50

另一个文件将显示不同的变量

Different Variables
var1
var2 high low high
var4      80  100

我能想到的是得到类似的东西:

common_var_and_val = {'var3': '50'}
diff_var_and_val = {'var1': (' ',' ',' '), 'var2': ('high', 'low', 'high'), 'var4': (' ','80''100')}

请注意,diff_var_and_val会告诉我d1,d2和d3中变量的值是什么(如果不存在变量,值将是一个空间),那么该值的顺序很重要(高,低,低,低,低,high = d1,d2,d3中的值。该值可以是字符串,整数等。我正在使用Python 2.7

我的问题:

a)如何获得common_var_and_val和diff_var_and_val?有没有 更好的方法来做我想做的事?

b)我该怎么办,如果我在excel中打开输出文件,它将 显示与我上面提到的完全一样。

这是第一个问题的答案(这是一个更一般的答案 - 函数可以接收任意数量的字典)。最后,有一个如何使用代码的示例。

基本上,如果键在所有字典中,则该函数检查每个密钥所有词典 - 如果键不在字典中,则值为"(否则是实际值...)

def f(*args):
    if args == []: return {},{}
    all_keys = set([])
    for dictionary in args:
        for keys in dictionary:
            all_keys.add(keys)
    common, diff = {},{}
    for k in all_keys:
        if check_equal_key(args,k):
            common[k] = [dictionary[k] for dictionary in args]
        else:
            diff[k]= []
            for dictionary in args:
                diff[k].append(dictionary[k] if k in dictionary else '')
    return common,diff
def check_equal_key(dict_list, k):
    '''this function check if a key is in all the dicts and if yes check if the value in equal in all the dictionaries'''
    if False in [True if k in dictionary else False for dictionary in dict_list]: return False
    prim_value = dict_list[0][k]
    for dictionary in dict_list[1:]:
        if prim_value != dictionary[k]: return False
    return True
a = {1:123,2:1,65:'as'}
b = {1:123,2:2,65:'asa'}
c = {1:123,2:1,67:'as'}
common,diff = f(a,b,c)
print common,'rn',diff

对于第二个问题:(主要函数是接收2个字典(最后一个答案的输出)的'f2',然后将其写入名为expenses01.xlsx的excel文件。在Anaconda中):

import xlsxwriter
def f2(common,diff):
    # Create a workbook and add a worksheet.
    workbook = xlsxwriter.Workbook('Expenses01.xlsx')
    worksheet = workbook.add_worksheet()
    # Start from the first cell. Rows and columns are zero indexed.
    row = 0
    col = 0
    worksheet.write(row,col,'common values:')
    row += 1
    row = write_dict(common,worksheet,row)   #write the 'common' dict
    worksheet.write(row, col, 'different values:')
    row += 1
    row = write_dict(diff,worksheet,row)     #write the diff' dict
    workbook.close()
def write_dict(dictionary,worksheet,row):
    '''this function write the dict in the excel file
    each key in a different row each value separated by a column, the function return the current row'''
    col = 0
    for k in dictionary:
        worksheet.write(row, col, k)
        for value in dictionary[k]:
            col += 1
            worksheet.write(row, col, value)
        col = 0
        row += 1
    return row
common = {1: [123, 123, 123]}
diff = {65: ['as', 'asa', ''], 2: [1, 2, 1], 67: ['', '', 'as']}
f2(common,diff)

基本代码是从这里获取的。

edit:当您不想使用任何模块时,可以使用以下代码,该代码可以创建一个新的TXT文件,该文件使用Excel打开时,Excel将显示数据就像你想要的。为此,每一行都用" n"分开,每个列带有一个' t',每个值都在double quot(例如")内,最后有一个有关如何使用的示例。

(如果您要问我,我建议使用图书馆...

def create_excel_txt_file_data(common,diff,file_path_and_name):
    '''create the data to be written in the txt file in excel format'''
    file_data = ''
    file_data+='"{}"n'.format('common values:')   #file data will be equal "common values:"n (with th quots)
    file_data+=write_dict(common)
    file_data += '"{}"n'.format('different values:')
    file_data += write_dict(diff)
    with open(file_path_and_name, 'w') as f:
        f.write(file_data)
def write_dict(dictionary):
    '''this function write the dict
    each key in a different row each value separated by a column'''
    data = ''
    for k in dictionary:
        data += '"{}"'.format(str(k))
        for value in dictionary[k]:
            data += 't"{}"'.format(str(value))   #between each colomn is a tab (t)
        data += 'n'
    return data
common = {1: [123, 123, 123]}
diff = {65: ['as', 'asa', ''], 2: [1, 2, 1], 67: ['', '', 'as']}
create_excel_txt_file_data(common, diff, 'Book1.txt')

我希望我有帮助。

由于常见和独特的词典具有多个值,我更喜欢将它们存储在以下数组中:

def get_vals(key, *dicts):
    vals = []
    for d in dicts:
        try:
            vals.append(d[key])
        except:
            pass
    return vals
def diff(*d):    
    common, distinct = {}, {}    
    keys_d = set([key for dict in d for key in dict.keys()])
    # Iterate through available keys to find common and distinct
    for key in keys_d:    
        values = get_vals(key, *d)          
        # If key present in all dicts and has a unique value across
        if len(values) == len(d) and len(set(values)) == 1:            
            common[key] = [d[0][key]]
        else:
            distinct[key] = values
    print common 
    print distinct

结果:

d1 = {'a':50, 'b':'', 'c':50}
d2 = {'c':40, 'a':'', 'b':'', 'e': 'abc'}
d3 = {'b':'', 'a':'', 'c':50, 'd': 'ijk'}
diff(d1,d2,d3)
{'b': ['']}
{'a': [50, '', ''], 'c': [50, 40, 50], 'e': ['abc'], 'd': ['ijk']}

欢呼!

最新更新