-6

I need some assistance with this Python problem.

I have a table called Matrix -

Matrix = [[['23'],['47'],['35'],['-']],
          [['45'],['22'],['34'],['-']],
          [['11'],['43'],['22'],['-']]]

What I would like to do is -

Remove/Delete an entire column IF all of it's cell values contain a null value like "-".

I already have a function which deletes an entire COLUMN by its index, but before I can do that I need to find a way of knowing if every cell in that particular column contain "-" before I can delete it.

Locating "-" -

for i, row in enumerate(Matrix):
    for x, col in enumerate(row):
        print Matrix[i][x], i, x

Output -

['23'] 0 0

['47'] 0 1

['35'] 0 2

['-'] 0 3

['45'] 1 0

['22'] 1 1

['34'] 1 2

['-'] 1 3

['11'] 2 0

['43'] 2 1

['22'] 2 2

['-'] 2 3

from this I can see that "-" does exist in each cell within column 3.

my attempt -

    for i, row in enumerate(Matrix):
        for x, col in enumerate(row):
            if "-" in col:
                print "Column to Delete is", x

p.s. I know I would have to do this in reverse, but I'm more interested in the logic of this.

4

2 回答 2

0

如果你需要自己做,你会怎么做?用英语讲

读取一列,如果该列只包含“-”删除它

阅读一列:如果您遇到“-”以外的其他内容,则在 Matrix[0-N][column number] 上循环将变量设置为 false

删除一列:在 Matrix[0-n][column number] 上循环并删除每个元素

您需要了解以下内容:

How to know the number of lines : len(Matrix)
how to know the number of column : len(Matrix[0])
How to remove a element : del matrix[rowNumber][columnNumber]

这为您提供了以下内容(比新手的其他遮阳篷更容易理解):

# --*-- encoding: iso-8859-1 --*--

def getMatrixNumberOfRows(Matrix):
    return len(Matrix)

def getMatrixNumberOfColumns(Matrix):
    return len(Matrix[0])

def mustColumnBeDeleted(Matrix, columnNumber):
    mustBeDeleted = True
    for rowNumber in range(0, getMatrixNumberOfRows(Matrix)):
        if Matrix[rowNumber][columnNumber] != ['-']:
            mustBeDeleted = False
    return mustBeDeleted

def deleteColumn(Matrix, columnNumber):
    for rowNumber in range(0, getMatrixNumberOfRows(Matrix)):
        del Matrix[rowNumber][columnNumber]

def printMatrix(Matrix):
    for row in Matrix:
        print row

#program

Matrix = [[['23'],['47'],['35'],['-']],
          [['45'],['22'],['34'],['-']],
          [['11'],['43'],['22'],['-']]]

for columnNumber in range(0, getMatrixNumberOfColumns(Matrix)):
    if mustColumnBeDeleted(Matrix, columnNumber) == True:
        deleteColumn(Matrix, columnNumber)

printMatrix(Matrix)

输出 :

[['23'], ['47'], ['35']]
[['45'], ['22'], ['34']]
[['11'], ['43'], ['22']]
于 2013-08-02T11:30:14.083 回答
0

好的,既然我们已经看到了您的尝试,我建议您掌握 Python 的一些概念,并了解 Python 的库和内置函数,尤其是在这种情况下,您需要了解zipall。您还需要了解可迭代和列表理解。

*Zip 是一个很棒的功能,在转置 Matrix 时可以与 splat 运算符一起使用。在这种情况下,由于您可以通过索引而不是列访问行(与 Numpy 不同),因此您的第一次尝试应该是转置数据。

>>> Matrix = [[['23'],['47'],['35'],['-']],
          [['45'],['22'],['34'],['-']],
          [['11'],['43'],['22'],['-']]]
>>> zip(*Matrix)
[(['23'], ['45'], ['11']), (['47'], ['22'], ['43']), (['35'], ['34'], ['22']), (['-'], ['-'], ['-'])]

一旦您将所有 Column 数据放在一行中,您的下一个尝试应该是找出哪一行包含所有 ['-']。如果所有元素都为真,您可以使用返回 true的内置函数all 。

您还需要知道如何浏览转置矩阵中的行。列表理解和/或生成器表达式很方便

[row for row in zip(*Matrix) if all(e != ['-'] for e in row)]

这是以下循环的等效简洁表示法

_Matrix = []
for row in  zip(*Matrix):
    #all(e != ['-'] for e in row)
    _cond = True
    for e in row:
        if e != ['-']:
               _cond = False
               break
    if not _cond:
        _Matrix.append(e)

现在,一旦您删除了目标行,您需要将数据转回以获取原始表单,删除所有 ['-'] 的列

zip(*(row for row in zip(*Matrix) if all(e != ['-'] for e in row)))
于 2013-08-02T11:28:19.330 回答