如果元组之间只有第一个索引匹配,则将重复项保留在元组列表中

Keep duplciate items in list of tuples if only the first index matches between the tuples

输入[(1,3), (3,1), (1,5), (2,3), (2,4), (44,33), (33,22), (44,22), (22,33)]

预期输出 [(1,3), (1,5), (2,3), (2,4), (44,33), (44,22)]

我正在尝试弄清楚以上内容并尝试了很多东西。到目前为止,我唯一的成功是,

for x in range(len(list1)):
if list1[0][0] == list1[x][0]:
    print(list1[x])

输出:(1, 3) \n (1, 5)

如有任何建议或帮助,我们将不胜感激。

使用以第一个值作为键控的 collections.defaultdict(list),并仅保留最终重复的值:

from collections import defaultdict  # At top of file, for collecting values by first element
from itertools import chain          # At top of file, for flattening result

dct = defaultdict(list)
inp = [(1,3), (3,1), (1,5), (2,3), (2,4), (44,33), (33,22), (44,22), (22,33)]
# For each tuple
for tup in inp:
   first, _ = tup  # Extract first element (and verify it's actually a pair)
   dct[first].append(tup)  # Collect with other tuples sharing the same first element

# Extract all lists which have two or more elements (first element duplicated at least once)
# Would be list of lists, with each inner list sharing the same first element
onlydups = [lst for firstelem, lst in dct.items() if len(lst) > 1]

# Flattens to make single list of all results (if desired)
flattened_output = list(chain.from_iterable(onlydups))

重要的是,这不需要有序输入,并且可以很好地扩展,做 O(n) 工作(天真地扩展你的解决方案会产生 O(n²) 解决方案,对于更大的输入来说要慢得多)。

另一种方法如下:

def sort(L:list):
    K = []
    for i in L :
        if set(i) not in K :
            K.append(set(i))
    output = [tuple(m) for m in K]
    return output

输出:

[(1, 3), (1, 5), (2, 3), (2, 4), (33, 44), (33, 22), (44, 22)]