以下是一个Python示例代码,用于按组删除连续相同的行:
def remove_consecutive_duplicates(matrix):
result = []
for row in matrix:
if result and result[-1] == row: # 检查当前行是否与上一行相同
continue
result.append(row)
return result
# 示例用法
matrix = [
[1, 2, 3],
[1, 2, 3],
[4, 5, 6],
[4, 5, 6],
[7, 8, 9]
]
result = remove_consecutive_duplicates(matrix)
for row in result:
print(row)
输出结果为:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
在这个示例中,remove_consecutive_duplicates
函数遍历给定的矩阵,如果当前行与上一行相同,则跳过当前行,否则将当前行添加到结果中。最后,返回结果矩阵并打印每一行。
下一篇:按组删除某一行号/条件下方的行