0
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 之间的值进行比较。用一个例子来解释。示例:group1 和 group2 中的 keys_to_compare = {'name', 'num',working}。

4

3 回答 3

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 语句以对具有相同值的元素执行您想要的操作。

于 2013-07-18T18:50:03.030 回答
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]]
于 2013-07-18T18:58:43.367 回答
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)
于 2013-07-18T18:59:01.580 回答