Python:配对匹配元素

时间:2018-06-07 12:17:00

标签: python

我有以下数据结构:

a = [('customerA', '1.0.0'), ('customerB', '1.0.0'), ('customerC', '1.0.1')]
b = (('customerB', '1.1.0'), ('customerC', '1.0.1'))

我希望结果是这样的:

[('customerA', None), ('customerB', '1.0.0', '1.1.0'), ('customerC', '1.0.1', '1.0.1')]

甚至完全跳过非现有客户:

[('customerB', '1.0.0', '1.1.0'), ('customerC', '1.0.1', '1.0.1')]

zip函数在这种情况下无效,因为b来自MySQLCursor.fetchall()且客户名称为WHERE的子句,因此它与{a不匹配1}}如果客户不存在:

>>> [a + (b[1],) for a, b in zip(a, b)]
[('customerA', '1.0.0', '1.1.0'), ('customerB', '1.0.0', '1.0.1')]
>>> import itertools
>>> for a, b in itertools.zip_longest(a, b):
...     print(a, b)
... 
('customerA', '1.0.0') ('customerB', '1.1.0')
('customerB', '1.0.0') ('customerC', '1.0.1')
('customerC', '1.0.1') None

5 个答案:

答案 0 :(得分:7)

您是否尝试直接进行此操作?

customers_a = dict(a)
result = [(customer, customers_a[customer], version) for customer, version in b if customer in customers_a]

现在,result正是

>>> result
[('customerB', '1.0.0', '1.1.0'), ('customerC', '1.0.1', '1.0.1')]

答案 1 :(得分:4)

使用collections

<强>演示:

import collections
a = [('customerA', '1.0.0'), ('customerB', '1.0.0'), ('customerC', '1.0.1')]
b = (('customerB', '1.1.0'), ('customerC', '1.0.1'))

checkDict = dict(b)
d = collections.defaultdict(list)
for i in (a + list(b)):
    if i[0] in checkDict.keys():
        d[i[0]].append(i[1])
print(d)

<强>输出:

defaultdict(<type 'list'>, {'customerC': ['1.0.1', '1.0.1'], 'customerB': ['1.0.0', '1.1.0']})

答案 2 :(得分:3)

In [11]: a = [('customerA', '1.0.0'), ('customerB', '1.0.0'), ('customerC', '1.0.1')]
    ...: b = (('customerB', '1.1.0'), ('customerC', '1.0.1'))

In [12]: ad = dict(a)

In [13]: bd = dict(b)

In [14]: [(k, ad.get(k), bd.get(k)) for k in set(ad.keys()) & set(bd.keys())]
Out[14]: [('customerC', '1.0.1', '1.0.1'), ('customerB', '1.0.0', '1.1.0')]

答案 3 :(得分:3)

您可以随时尝试itertools.product

>>> from itertools import product
>>> x = [('customerA', '1.0.0'), ('customerB', '1.0.0'), ('customerC', '1.0.1')]
>>> y = (('customerB', '1.1.0'), ('customerC', '1.0.1'))
>>> [(a, b, d) for (a, b), (c, d) in product(x, y) if a == c]
[('customerB', '1.0.0', '1.1.0'), ('customerC', '1.0.1', '1.0.1')]

注意:这假设两个数据结构之间只存在一个客户对。

答案 4 :(得分:2)

如果您要排除仅在a中的客户,您可以使用列表理解来完成此操作:

a = [('customerA', '1.0.0'), ('customerB', '1.0.0'), ('customerC', '1.0.1')]
b = (('customerB', '1.1.0'), ('customerC', '1.0.1'))

result = [(ca, x, y) for (ca, x) in a for (cb, y) in b if ca == cb]
# [('customerB', '1.0.0', '1.1.0'), ('customerC', '1.0.1', '1.0.1')]