Python 如何比较同一词典中多个键的值?

Python 如何比较同一词典中多个键的值?,python,dictionary,Python,Dictionary,应用:我们的产品可以有数百种不同的性能。当我们必须为tens产品提供服务时,该应用程序可以通过收集具有相同配置的产品来帮助我们减少服务数量 工作原理:用户在条目中提供属性,应用程序将比较这些属性,以查看某些配置是否相同(此处的configuration=“Config 0”或“Config 1”等…) 配置和给定的属性应该放在一个字典中,如{Config 1:[1800,11500,x..],Config 2:[2000,21600,x..],Config n:[x,y,o,x..]}程序将比较

应用:我们的产品可以有数百种不同的性能。当我们必须为tens产品提供服务时,该应用程序可以通过收集具有相同配置的产品来帮助我们减少服务数量

工作原理:用户在条目中提供属性,应用程序将比较这些属性,以查看某些配置是否相同(此处的configuration=“Config 0”或“Config 1”等…)

配置和给定的属性应该放在一个字典中,如{Config 1:[1800,11500,x..],Config 2:[2000,21600,x..],Config n:[x,y,o,x..]}程序将比较键的值列表,并尝试检查某些值列表是否相同

问题:我没有设法比较字典的值,以了解是否有具有相同值的键

我怎样才能解决这个问题

按钮“启动”可启动比较

import os
from tkinter import *
from tkinter import ttk


data_list=['Name','Q','Speed','TH','Open through','Level','Accesses','CW','CD','CH','SW','SD','Headroom','SP','Door type','DW','DH']
list_excel=[]
dict_user_entries={}
dict_redundance={}

fenetre =Tk()
fenetre.geometry('800x750')
a = 5
nbofconfig=4

def comparison():
    dict_redundance={}
    for r in range(len(dict_user_entries)):
        for o in range(len(dict_user_entries)):
            if dict_user_entries['Config '+str(r)] == dict_user_entries['Config '+str(o)]: #---  We compare if the values of the compared keys are the same
                print('yes -- Config ',str(r),' = Config ',str(o))
                if  'Config '+str(o) in dict_redundance:
                    dict_redundance['Config '+str(o)].append('Config '+str(r))
                else:
                    dict_redundance['Config '+str(o)]=['Config '+str(r)]
                
            else:
                print('no -- Config ',str(r),' =x= Config ',str(o))
                
    #----- print
    for i in dict_redundance.keys():
        print(i)
        
            

    
cadrebouton=Frame(fenetre)
cadrebouton.pack(side=TOP,anchor=NW)
boutonexcel=Button(cadrebouton,text="Load Excel File",width=15,height=1,bg="white",bd=5, command=excel_load)
boutonexcel.pack(side=LEFT, anchor =NW)
boutontest=Button(cadrebouton,text="Test",width=15,height=1,bg="white",bd=5, command=test)
boutontest.pack(side=LEFT, anchor =NW)
boutonlaunch=Button(cadrebouton,text="Launch",width=15,height=1,bg="white",bd=5, command=comparison)
boutonlaunch.pack(side=LEFT, anchor =NW)

head_frame=Frame(fenetre)
head_frame.pack(side=TOP,anchor=NW)

head_label=Label(head_frame,text='Data',width=15)
head_label.pack(side=LEFT)

for i in range(nbofconfig):
    head_config=Label(head_frame,text='Config '+str(i), width=13)
    head_config.pack(side=LEFT)


cadre_global=Frame(fenetre)
cadre_global.pack(side=TOP,anchor=NW)

cadre_global2=Frame(cadre_global)
cadre_global2.pack(side=LEFT)


for x in range(a):
    frame=Frame(cadre_global2)
    frame.pack(side=TOP,anchor=NW)
    
    label_data=Label(frame,width=15,text=data_list[x])
    label_data.pack(side=LEFT)
    
cadre_global3=Frame(cadre_global)
cadre_global3.pack(side=LEFT)
bx=0#------Test, to delete



    
for i in range(nbofconfig):

    frame2=Frame(cadre_global3)
    frame2.pack(side=LEFT)
    dict_user_entries['Config '+str(i)]=[]
    for x in range(a):
        bx+=x
        var_entry=StringVar()
        my_entry=Entry(frame2,textvariable=var_entry, width=15,bd=2) #--- Users gives the property
        my_entry.insert(0,bx)
        my_entry.pack()
        dict_user_entries['Config '+str(i)].append(my_entry) #--- Data from user will stored in the dictionnary
     
    
fenetre.mainloop()

os.system('pause')
这里有一个例子:

谢谢大家!

编辑2021年5月11日:我找到了解决方案。只需将dict_user_条目中的键和值与.get()一起放入一个新字典

def comparison():
    list_values=[]
    dict_2={}
    dict_reference={}
    dict_final={}
    
    #---- We put the data of entries in a new dictionnary
    index_1=0
    for keys in dict_user_entries.keys():
        dict_2[list_name[index_1].get()]=[]
        for i in range(a):
            dict_2[list_name[index_1].get()].append(dict_user_entries[keys][i].get()) 
        index_1+=1
    
    #---- We stored value in a list to count the number of different configurations (len(list_values)) and create intermediate dictionaries
    index=0
    for keys in dict_2.keys():
        if dict_2[keys] in list_values:
            pass
        else:
            list_values.append(dict_2[keys])
            dict_reference['Config '+str(index)]=dict_2[keys]
            dict_final['Config '+str(index)]=[]
            index+=1
            
    #---- We create the unique configurations with their datas
    index_2=0
    for keys in dict_2.keys():
        for i in dict_reference.keys():
            if dict_2[keys] == dict_reference[i]:
                dict_final[i].append(list_name[index_2].get())
            else:
                pass
        index_2+=1        
                
    #----- print
    for keys in dict_final.keys():
        print('\n',keys,' = ',dict_reference[keys],' = ', dict_final[keys])

看起来您正在比较的是tkinter条目实例的列表。您需要做的是从条目中提取值,然后比较这些值。不能对整个列表进行比较,因为比较的是列表对象本身,而不是值

替换此行:

if dict_user_entries['Config '+str(r)] == dict_user_entries['Config '+str(o)]:
为此:

list_a = [x.get() for x in dict_user_entries['Config '+str(r)]]
list_b = [x.get() for x in dict_user_entries['Config '+str(o)]]
if all([list_a[i] == list_b[i] for i in range(len(list_a))]):

嗨,谢谢。我已经用类似的解决方案编辑了我的文章,比你的文章早了几分钟。你说得对!我刚刚提取了条目的值(.get()),并将它们放入一个新字典中。经过多次测试后,该应用程序现在可以正常工作:)