简体   繁体   English

将两个字典值与列表中的键对应

[英]Comapring two dictionary values with keys which are in list

group1=  [ {
              'Name': 'C21114',
              'Description': '',
              'num': '12321114',
              'working': 'true',
              'belongs': 'Default',
              'Expiry_Date': '',
              '\xef\xbb\xbfUser_ID': 'C21114',
              'Password': '*SECRET*',
          },
          {
              'Name': 'Mahes',
              'Description': '',
              'num': '1026',
              'working': 'true',
              'belongs': 'Default',
              'Expiry_Date': '',
              '\xef\xbb\xbfUser_ID': 'Mahi',
              'Password': '*SECRET*',
          },
          {
              'Name': 'pri',
              'Description': '',
              'num': '1027',
              'working': 'true',
              'belongs': 'Default',
              'Expiry_Date': '',
              '\xef\xbb\xbfUser_ID': 'priya',
              'Password': '*SECRET*',
          }]
group2=   [{
              'Name': 'C21114',
              'Description': '',
              'num': '12321114',
              'working': 'true',
              'belongs': 'Default',
              'Expiry_Date': '',
              'User_ID': 'C21114',
              'Password': '*SECRET*',
          },
          {
              'Name': 'Mahes',
              'Description': '',
              'num': '1026',
              'working': 'true',
              'belongs': 'Default',
              'Expiry_Date': '',
              'User_ID': 'Mahi',
              'Password': '*SECRET*',
          },
          {
              'Name': 'pri',
              'Description': '',
              'num': '1027',
              'working': 'true',
              'belongs': 'Default',
              'Expiry_Date': '',
              'User_ID': 'priya',
              'Password': '*SECRET*',
          }]

Need to compare few keys of group1 and group2 are same or not. 需要比较group1和group2的几个键是否相同。 group1 and group2 are list in that many dictionaries.I just need to compare few keys with its values between group1 and group2.Explained with one example.Example : keys_to_compare = {'name', 'num',working} from group1 and group2. group1和group2是许多词典中的列表。我只需要比较group1和group2之间的几个键及其值。用一个示例进行解释。示例:group1和group2中的keys_to_compare = {'name','num',working}。

This should do what you need it to do 这应该做您需要做的

key_to_compare = ['Name', 'num', 'working']
for key in key_to_compare:
    for d1 in group1:
        for d2 in group2:
            if d1[key] == d2[key]:
                print "same values for %s %s %s" % (key, d1[key], d2[key])

Change the if statement to do what you would like for elements with the same value. 更改if语句以对具有相同值的元素执行所需操作。

I had to make an assumption on what the output you wanted. 我必须对您想要的输出进行假设。 I created a list of lists. 我创建了一个列表列表。 The inner most list is a part of indexs (group1 then group2) of matches. 最里面的列表是匹配的索引的一部分(第1组然后是第2组)。 This is the code: 这是代码:

keys_to_compare = ['Name','num','working']
matches = []
for idx1 in range(len(group1)):
    ref1 = group1[idx1]
    found = False
    for idx2 in range(len(group2)):
        ref2 = group2[idx2]
        found = True
        for key in keys_to_compare:
            if ref1[key] != ref2[key]:
                found = False
        if found:
            matches.append([idx1,idx2])
            break
    if found: continue
print 'matches=%r' % (matches)

The result: 结果:

matches=[[0, 0], [1, 1], [2, 2]]

Implemented as a function, this should give you what you want: 作为一个函数实现,这应该给您您想要的东西:

def compare(key):
    g1 = []
    g2 = []
    for i in group1:
        g1.append(i[key])
    for j in group2:
        g2.append(j[key])
    return g1 == g2

Put in the key name, then it will return True if the values are same in both groups and False if not. 输入密钥名称,然后如果两个组中的值相同,则返回True,否则返回False。 For instance, to check the keys in your list, you'll do: 例如,要检查列表中的键,您将执行以下操作:

keys_to_compare = ['Name','num','working']

for x in keys_to_compare:
    print compare(x)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM