我想比较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中打开输出文件 显示与我上面提到的完全一样。
答案 0 :(得分:2)
以下是第一个问题的答案(这是一个更一般的答案 - 该函数可以接收任意数量的字典)。最后有一个如何使用代码的例子。
基本上,如果一个键在所有字典中,该函数检查每个键,如果是,并且所有字典中的值相等,它将它存储在'common'字典中,如果不是,则函数检查是关键是所有字典 - 如果键不在字典中,则值为''(否则它是真正的值...)
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,'\r\n',diff
对于第二个问题:(主要功能是'f2'接收2个字典(最后一个答案的输出)并将其写入名为Expenses01.xlsx的excel文件。请注意,您将需要xlsxwriter模块(isady encalled)在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)
基本代码取自here,您或许应该检查一下。
编辑当您不想使用任何模块时,可以使用以下代码执行以下操作:它创建一个新的txt文件,当使用excel打开时,excel将显示数据就像你想要的那样为此,每一行用'\ n'分隔,每列用一个标签'\ t',每个值在双引号(例如“)内,最后有一个如何使用的例子。
(如果你问我,我会推荐使用这个库......)
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')
我希望我能帮忙。
答案 1 :(得分:1)
由于常用和不同的词典包含多个值,我更喜欢将它们存储在数组中,如下所示:
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']}
干杯!