📜  Python - 删除重复的子集元组

📅  最后修改于: 2022-05-13 01:55:32.378000             🧑  作者: Mango

Python - 删除重复的子集元组

有时,在使用Python元组时,我们可能会遇到需要执行删除元组的问题,这些元组已经作为子集存在于其他元组中。这种问题在数据预处理中很有用。让我们讨论可以执行此任务的某些方式。

方法 #1:使用 setdefault() + 列表推导
这是可以解决此任务的方法之一。在此,我们执行初始化列表并保持元素进行比较的任务。最后,列表推导用于执行子集元组的删除。这种方法提供了删除元组大小的灵活性。

Python3
# Python3 code to demonstrate working of
# Remove Duplicate subset Tuples
# Using setdefault() + list comprehension
 
# initializing lists
test_list = [(6, 9, 17, 18), (15, 34, 56), (6, 7), (6, 9), (15, 34)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 2
 
# Remove Duplicate subset Tuples
# Using setdefault() + list comprehension
temp = {}
for sub in test_list:
    temp2 = sub[:K]
    temp.setdefault(temp2, []).append(sub)
res = [sub for sub in test_list if len(sub) > K or len(temp[sub]) == 1]
 
# printing result
print("Tuple list after removal : " + str(res))


Python3
# Python3 code to demonstrate working of
# Remove Duplicate subset Tuples
# Using all() + any()+ loop
 
# initializing lists
test_list = [(6, 9, 17, 18), (15, 34, 56), (6, 7), (6, 9), (15, 34)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Remove Duplicate subset Tuples
# Using all() + any() + loop
res = []
test_list = sorted(test_list, key = lambda x: len(x))
for idx, sub in enumerate(test_list):
    if any(all(ele in sub2 for ele in sub) for sub2 in test_list[idx + 1:]):
        pass
    else:
        res.append(sub)
 
# printing result
print("Tuple list after removal : " + str(res))


输出 :
The original list is : [(6, 9, 17, 18), (15, 34, 56), (6, 7), (6, 9), (15, 34)]
Tuple list after removal : [(6, 9, 17, 18), (15, 34, 56), (6, 7)]


方法 #2:使用 all() + any() + 循环
上述功能的组合提供了另一种解决这个问题的方法。在此,我们测试所有子集,而不考虑大小。 any()函数用于检查在使用 all() 提取的特定元组的所有元素中是否有任何元组是新的。

Python3

# Python3 code to demonstrate working of
# Remove Duplicate subset Tuples
# Using all() + any()+ loop
 
# initializing lists
test_list = [(6, 9, 17, 18), (15, 34, 56), (6, 7), (6, 9), (15, 34)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Remove Duplicate subset Tuples
# Using all() + any() + loop
res = []
test_list = sorted(test_list, key = lambda x: len(x))
for idx, sub in enumerate(test_list):
    if any(all(ele in sub2 for ele in sub) for sub2 in test_list[idx + 1:]):
        pass
    else:
        res.append(sub)
 
# printing result
print("Tuple list after removal : " + str(res))
输出 :
The original list is : [(6, 9, 17, 18), (15, 34, 56), (6, 7), (6, 9), (15, 34)]
Tuple list after removal : [(6, 9, 17, 18), (15, 34, 56), (6, 7)]