找出Python中2个列表之间的区别

时间:2013-11-30 06:29:22

标签: python python-2.7

如何通过比较一个对象的属性来找到2个对象之间的差异?

在这个例子中,如果phone属性相同,则说2个对象彼此相等。

a1 = {'name':'Tom', 'phone':'1234'}
a2 = {'name':'Dick', 'phone':'1111'}
a3 = {'name':'Harry', 'phone':'3333'}
a = [a1,a2,a3]

b1 = {'name':'Jane', 'phone':'1234'}
b2 = {'name':'Liz', 'phone':'2222'}
b3 = {'name':'Mary', 'phone':'4444'}
b = [b1,b2,b3]

def check(x, y):
    if(x['phone'] == y['phone']):
        return True
    else:
        return False

期望的结果应该是:

result_A_minus_B = [a2, a3]
result_B_minus_A = [b2, b3]

我在下面的尝试会引发错误TypeError: list indices must be integers, not str

[x for x in a if check(a,b)]

3 个答案:

答案 0 :(得分:5)

使用给定的数据结构,您必须反复遍历第二个词典列表中的项目,这是相对低效的。您关心的是第二个词典列表中是否已存在给定的电话号码。用于重复测试给定值是否存在的最有效数据结构是set(或dict,如果您可能需要从电话号码索引回到更多信息)。所以我会这样做:

a = [a1, a2, a3]
b = [b1, b2, b3]
a_phone_numbers_set = set(d['phone'] for d in a])
b_phone_numbers_set = set(d['phone'] for d in b])
result_A_minus_B = [d for d in a if d['phone'] not in b_phone_numbers_set]
result_B_minus_A = [d for d in b if d['phone'] not in a_phone_numbers_set]

或者,如果我想创建一个函数:

def unmatched_entries(list1, list2):
    existing_entries = set(d['phone'] for d in list2)
    return [d for d in list1 if d['phone'] not in existing_entries]

或者,您可以使用任意键:

def unmatched_entries(list1, list2, matching_key):
    existing_entries = set(d[matching_key] for d in list2 if matching_key in d)
    return [d for d in list1 if matching_key in d and d[matching_key] not in existing_entries]

该版本始终会跳过list1中未定义所请求密钥的条目 - 其他行为也是可能的。

为了匹配短暂出现的评论所提到的多个键,我会使用一组值的元组:

a_match_elements = set((d['phone'], d['email']) for d in a])
result_B_minus_a = [d for d in b if (d['phone'], d['email']) not in a_match_elements]

同样,这可以推广到处理一系列密钥。

答案 1 :(得分:0)

此功能:

def minus(list1, list2):
    return [x for x in list1 if x['phone'] not in set(y['phone'] for y in list2)]

给出了这些结果:

>>> minus(a, b)
[{'name': 'Dick', 'phone': '1111'}, {'name': 'Harry', 'phone': '3333'}]
>>> minus(b, a)
[{'name': 'Liz', 'phone': '2222'}, {'name': 'Mary', 'phone': '4444'}]

答案 2 :(得分:0)

如果您可以更改数据类型,则dict可能会更好。您可以使用电话号码作为检索姓名的密钥。

a = {'1234':'Tom','1111':'Dick','3333':'Harry'}
b = {'1234':'Jane', '2222':'Liz','4444':'Mary'}

def minus(x, y):
    {z:a[z] for z in set(x.keys()) - set(y.keys())}

# {'1111':'Dick','3333':'Harry'}
a_minus_b = minus(a, b)

# {'2222':'Liz','4444':'Mary'}
b_minus_a = minus(b, a)