Python - 删除重复元素的列
给定一个矩阵,编写一个Python程序以在任何行中出现重复项时删除整列。
例子:
Input : test_list = [[4, 3, 5, 2, 3], [6, 4, 2, 1, 1], [4, 3, 9, 3, 9], [5, 4, 3, 2, 1]]
Output : [[4, 3, 5], [6, 4, 2], [4, 3, 9], [5, 4, 3]]
Explanation : 1 has duplicate as next element hence 5th column is removed. 3 occurs as 2nd and 4th index, hence 4th index is removed.
Input : test_list = [[6, 4, 2, 1, 1], [4, 3, 9, 3, 9], [5, 4, 3, 2, 1]]
Output : [[6, 4, 2], [4, 3, 9], [5, 4, 3]]
Explanation : 1 has duplicate as next element hence 5th column is removed. 3 occurs as 2nd and 4th index, hence 4th index is removed.
方法:使用列表理解+ set() + chain.from_iterable() +生成器+ 循环
在这种情况下,使用生成器函数和 set() 作为第一步提取重复元素的索引。第二步,在重建Matrix时排除所有需要的列。
Python3
# Python3 code to demonstrate working of
# Remove Columns of Duplicate Elements
# Using list comprehension + set() +
# chain.from_iterable() + generator + loop
from itertools import chain
def dup_idx(sub):
memo = set()
for idx, ele in enumerate(sub):
# adding element if not there
if ele not in memo:
memo.add(ele)
else:
# return index is Duplicate
yield idx
# initializing list
test_list = [[4, 3, 5, 2, 3], [6, 4, 2, 1, 1],
[4, 3, 9, 3, 9], [5, 4, 3, 2, 1]]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 3
# passing each row to generator function
# flattening indices at end
temp_idxs = set(chain.from_iterable(dup_idx(sub) for sub in test_list))
# extracting columns with only non-duplicate indices values
res = [[ele for idx, ele in enumerate(
sub) if idx not in temp_idxs] for sub in test_list]
# printing result
print("The filtered Matrix : " + str(res))
输出:
The original list is : [[4, 3, 5, 2, 3], [6, 4, 2, 1, 1], [4, 3, 9, 3, 9], [5, 4, 3, 2, 1]]
The filtered Matrix : [[4, 3, 5], [6, 4, 2], [4, 3, 9], [5, 4, 3]]