2

在 python 中,我有两个列表AB. 两个列表都由元组组成(x,y)。例如:

A = [('x1','y1'), ('x2','y2'), ('x3','y3')]
B = [('x1','y1'), ('x2','y5'), ('x4','y4')]

现在,我想要三个结果。只要不涉及元组,所有这些都可以用集合论轻松解决。

结果 1:两个列表的交集:) set(A) & set(B)。所以结果应该是比较两个列表的元组的两个值。结果应该是:C = [('x1','y1')]

结果 2:两个列表的交集,只有(x,y)[0]匹配项。结果应该是:D = (('x1','y1'), ('x2', ('y2', 'y5'))]。理想情况下,解决方案是D - C -> E = [('x2', ('y2', 'y5'))],但我可以忍受拥有D自己。

结果 3: listBA:相比的唯一性set(B)-(set(A) & set(B))。仅在 上进行比较(x,y)[0]。结果应该是:[('x4', 'y4')]

我在这些问题上找不到任何东西,也无法自己构建解决方案。任何人都可以帮忙吗?

4

3 回答 3

1

为什么不使用 python 的set()?1 是非常直接的,2 是需要更多的工作:

A = [('x1','y1'), ('x2','y2'), ('x3','y3')]
B = [('x1','y1'), ('x2','y5'), ('x4','y4')]

a,b = set(A),set(B)
print '1:',a&b

axs = set(map(itemgetter(0),A))
bxs = set(map(itemgetter(0),B))

result2 = []
for c in axs&bxs:
    result2.append((c,set([y for x,y in A+B if x==c]))
print '2:',result2

输出:

1: set([('x1', 'y1')])
2: [('x2', set(['y2', 'y5'])), ('x1', set(['y1']))]

您可以对 3 使用非常相似的方法

于 2015-09-02T09:41:17.727 回答
1

以下是一些使用 dicts 做你想做的事情的方法。这是 Python 2 代码;它需要对 Python 3 进行一些小的修改。IIRC,Python 3 没有,dict.iteritems()因为它dict.items()返回一个迭代器而不是一个列表。

A = [('x1','y1'), ('x2','y2'), ('x3','y3')]
B = [('x1','y1'), ('x2','y5'), ('x4','y4')]

dA = dict(A)
dB = dict(B)

#Intersection, the simple way
print 'Result 1a:', list(set(A) & set(B))

#Intersection using dicts instead of sets
result = [(k, vA) for k, vA in dA.iteritems() if dB.get(k) == vA]
print 'Result 1b:', result

#match on 1st tuple element, ignoring 2nd element
result = {}
for k, vA in dA.iteritems():
    vB = dB.get(k)
    if vB:
        result[k] = (vA, vB) if vB != vA else vA
print 'Result 2a:', result.items()

#match on 1st tuple element only if 2nd elements don't match
result = {}
for k, vA in dA.iteritems():
    vB = dB.get(k)
    if vB and vB != vA:
        result[k] = (vA, vB)
print 'Result 2b:', result.items()

#unique elements of B, ignoring 2nd element
result = [(k, vB) for k, vB in dB.iteritems() if k not in dA]
print 'Result  3:', result

输出

Result 1a: [('x1', 'y1')]
Result 1b: [('x1', 'y1')]
Result 2a: [('x2', ('y2', 'y5')), ('x1', 'y1')]
Result 2b: [('x2', ('y2', 'y5'))]
Result  3: [('x4', 'y4')]
于 2015-09-02T10:55:07.397 回答
1
  1. 两个列表的交集:

    您已经知道解决方案:set(A) & set(B). 或者,等价地,set(A).intersection(B)

    >>> A = [('x1', 'y1'), ('x2', 'y2'), ('x3', 'y3')]
    >>> B = [('x1', 'y1'), ('x2', 'y5'), ('x4', 'y4')]
    >>> set(A).intersection(B)
    {('x1', 'y1')}
    
  2. 只有 (x,y)[0] 匹配的两个列表的交集:

    首先,确保两者AB都按它们的 x 坐标排序。

    然后使用itertools.groupby()和字典:

    >>> a_grouped = {x: list(points) for x, points in
    ...              itertools.groupby(A, lambda point: point[0])}
    >>> b_grouped = {x: list(points) for x, points in
    ...              itertools.groupby(B, lambda point: point[0])}
    >>> [(x, {point[1] for point in a_grouped[x] + b_grouped[x]})
    ...  for x in a_grouped if x in b_grouped]
    [('x2', {'y5', 'y2'}), ('x1', {'y1'})]
    

    (这和你问的不太一样,因为如你所见,我们有 ('x1', {'y1'})而不是('x1', 'y1')。此外,我们有集合而不是列表,但这些都是很容易解决的问题。)

    如果你想排除共同点:在调用 groupby() 之前AB之前删除它们:

    >>> A = set(A)
    >>> B = set(B)
    >>> common_points = A & B
    >>> A = [point for point in A if point not in common_points]
    >>> B = [point for point in B if point not in common_points]
    
  3. list 的唯一性B与 相比A,仅在 上进行比较(x,y)[0]

    构造 中点的所有 x 坐标的集合A

    >>> exclude = {point[0] for point in A}
    >>> [point for point in B if point[0] not in exclude]
    [('x4', 'y4')]
    

    请注意,元素exclude是的键a_grouped——这意味着您可以重用上一个问题中的部分代码并编写:

    >>> [point for point in B if point[0] not in a_grouped]
    [('x4', 'y4')]
    

对于所有这些解决方案,性能和可读性都可以提高,如果您要使用我的代码,请考虑这一点。

于 2015-09-02T11:56:53.827 回答