将两个字典值与列表中的键进行比较

时间:2013-07-18 18:41:52

标签: python dictionary

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*',
          }]

需要比较group1和group2的几个键是否相同。 group1和group2列在那么多字典中。我只需要比较几个键及其在group1和group2之间的值。用一个例子解释。例如:keys1to_compare =来自group1和group2的{'name','num',working}。

3 个答案:

答案 0 :(得分:0)

这应该做你需要做的事情

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])

更改if语句,使其具有相同值的元素。

答案 1 :(得分:0)

我必须假设你想要的输出。我创建了一个列表列表。最内部的列表是匹配的索引(group1然后是group2)的一部分。这是代码:

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)

结果:

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

答案 2 :(得分:0)

作为一个函数实现,这应该给你你想要的东西:

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

输入密钥名称,如果两个组中的值相同则返回True,否则返回False。例如,要检查列表中的键,您将执行以下操作:

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

for x in keys_to_compare:
    print compare(x)
相关问题