,可以通过以下步骤实现:
以下是一个示例代码,演示如何从2D列表中删除重复的后续条目:
def remove_duplicates(matrix):
seen = set()
result = []
for row in matrix:
unique_row = []
for item in row:
if item not in seen:
unique_row.append(item)
seen.add(item)
result.append(unique_row)
return result
# 示例用法
matrix = [[1, 2, 3], [4, 5, 6], [1, 2, 3], [7, 8, 9]]
result = remove_duplicates(matrix)
print(result)
# 输出: [[1, 2, 3], [4, 5, 6], [], [7, 8, 9]]
这个算法通过使用集合来记录已经出现过的元素,从而实现了删除重复的后续条目的功能。在示例中,原始的2D列表是[[1, 2, 3], [4, 5, 6], [1, 2, 3], [7, 8, 9]]
,经过处理后得到的结果是[[1, 2, 3], [4, 5, 6], [], [7, 8, 9]]
,其中重复的后续条目被删除了。
这个算法的时间复杂度是O(n*m),其中n是2D列表的行数,m是2D列表的列数。
领取专属 10元无门槛券
手把手带您无忧上云