删除重复项(元组的元组)

2024-05-08 07:27:24 发布

您现在位置:Python中文网/ 问答频道 /正文

输入数据:

input_tuple = (
            (1, 'name1', 'Noah'),
            (1, 'name2', 'Liam'),

            (2, 'name3', 'Mason'),

            (3, 'name4', 'Mason'),

            (4, 'name5', 'Noah'),
            (4, 'name6', 'Liam'),

            (5, 'name7', 'Elijah'),
            (5, 'name8', 'Noah'),
            (5, 'name9', 'Liam')
          )

转换成dict(键,值):

^{pr2}$

只是为了了解数据模型,还进行了一些筛选:

^{3}$

现在我想消除重复,然后恢复到如下所示的元组: 重复匹配条件: 1) 消除重复,如果长度(值)>1 2) 值应该完全匹配,而不是部分匹配。在

注: 键2和3的值不重复,因为len(value)不是-gt 1 关键字4的值已丢失,因为它完全重复 由于我们正在进行精确匹配,因此在键5中值['Noah',Liam]将不去。在

 output_tuple = 
      (
        (1, 'name1', 'Noah'),
        (1, 'name2', 'Liam'),

        (2, 'name3', 'Mason'),

        (3, 'name4', 'Mason'),

        (5, 'name7', 'Elijah'),
        (5, 'name8', 'Noah'),
        (5, 'name9', 'Liam')
      )

我试过的代码:

from functools import reduce
from collections import defaultdict

input_tuple_dictionary = defaultdict(list)
for (key, *value) in input_tuple:
    input_tuple_dictionary[key].append(value[1])

input_tuple_dictionary
for index in range(len(input_tuple_dictionary)-1):
    for key, value in input_tuple_dictionary.items():
        if len(value) > 1:
            if value == value[index+1]:
                print(key)

Tags: keyinforinputdictionarylenvalueliam
3条回答

这里有一个使用defaultdict对象和toolz.unique对象的defaultdicttoolz.unique相当于文档中可用的itertools^{} recipe。在

其思想是查找具有单独值的键以及不具有重复值的键。这两类的结合构成了你的结果。在

from collections import defaultdict
from toolz import unique

dd = defaultdict(set)

for k, _, v in input_tuple:
    dd[k].add(v)

lones = {k for k, v in dd.items() if len(v) == 1}
uniques = set(unique(dd, key=lambda x: frozenset(dd[x])))

res = tuple(i for i in input_tuple if i[0] in lones | uniques)

结果:

^{pr2}$

跳过重复项的一个常见解决方案是保留一个包含您已经看到的所有元素的集合。如果以前见过对象,则不会将其添加到结果中。在

棘手的一点是,您尝试取消复制的对象是驻留在集合中不同元组中的多个对象的聚合。使用groupby是将这些对象集中在一个方便的包中的有效方法。在

from itertools import groupby

input_tuple = (
    (1, 'name1', 'Noah'),
    (1, 'name2', 'Liam'),

    (2, 'name3', 'Mason'),

    (3, 'name4', 'Mason'),

    (4, 'name5', 'Noah'),
    (4, 'name6', 'Liam'),

    (5, 'name7', 'Elijah'),
    (5, 'name8', 'Noah'),
    (5, 'name9', 'Liam')
  )

seen = set()
result = []
for _, group in groupby(input_tuple, key=lambda t: t[0]):
    #convert from iterator to list, since we want to iterate this more than once
    group = list(group)
    #extract just the names from each tuple.
    names = tuple(t[2] for t in group)
    #check for duplicates, but only for name groups with more than one element.
    if len(names) == 1 or names not in seen:
        result.extend(group)
    seen.add(names)

print(result)

结果:

^{pr2}$
# Using the dict format of yours
data = [set(dict[x]) for x in range(1, len(dict) + 1)]
input_tuple = dict
seen = []
output_tuple = []
for i in range(len(data)):
    if (data[i] not in seen) or len(data[i]) == 1:
        for j in range(len(input_data)):
            if input_data[j][0] == i + 1:
                output_tuple.append(input_data[j])
    seen.append(data[i])
output_tuple = tuple(output_tuple)

如果你不明白,请问

祝你好运

相关问题 更多 >