简体   繁体   English

检查二维列表中的列是否包含相同的值

[英]Check if a column in a 2D list contains the same values

For example, a = [[1,2,3],[1,2,3],[1,2,3]] has 3 columns with the same values例如, a = [[1,2,3],[1,2,3],[1,2,3]]有 3 列具有相同的值

I'm trying to check How can I do this without using numpy arrays?我正在尝试检查如何在不使用numpy arrays 的情况下执行此操作? I essentially need help iterating over columns instead of rows.我基本上需要帮助迭代列而不是行。

You could iterate over the columns with a loop, and for each column check the all the row values are equal to the first rows:您可以使用循环遍历列,并为每一列检查所有行值是否等于第一行:

def numUniformColumns(mat):
    cnt = 0
    for j in range(0, len(mat[0])):
        first = mat[0][j]
        uniform = True
        for i in range(1, len(mat)):
            if first != mat[i][j]:
                uniform = False
                break;

        if uniform:
            cnt += 1

    return cnt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM