How to find repeated rows of a matrix in Python

1 Answer

0 votes
def row_to_string(row):
    return ",".join(map(str, row))

def find_repeated_rows(matrix):
    row_count = {}

    for row in matrix:
        pattern = row_to_string(row)
        row_count[pattern] = row_count.get(pattern, 0) + 1

    print("Repeated Rows:")
    for pattern, count in row_count.items():
        if count > 1:
            print(f"Row: [{pattern}] - Repeated {count} times")

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [1, 2, 3],
    [7, 8, 9],
    [4, 5, 6],
    [0, 1, 2],
    [4, 5, 6]
]

find_repeated_rows(matrix)




'''
run:

Repeated Rows:
Row: [1,2,3] - Repeated 2 times
Row: [4,5,6] - Repeated 3 times

'''

 



answered May 24, 2025 by avibootz
...