检查矩阵中的列或对角线是否等于某事

时间:2017-03-26 15:56:25

标签: python matrix

我可以使用此代码检查矩阵中的行是否为x:

q = [[1,2,1],[1,2,1],[2,1,2]]
answer = [sum(row) for row in q]
for i in range(0, len(q)):
    if answer[i] == 6:
        print "Player 2 won!"
    if answer[i] == 3:
        print "Player 1 won!"
    if answer[i] != 6 and 3:
        print "It's a tie!"

如何检查我的矩阵是否具有= x的对角线或列,而不使用Numpy(是否有数学方法如上所示?)

示例:(X =无关紧要的事情)

q = [[1,2,1] [1,1,1] [1,1,2]] 应打印[4,3,5](第2行的总和)

[[1,X,X],
[X,1,X]
[X,X,1]]

应打印Diagonal

q = [[X,X,1] [X,1,X] [1,X,X]] 应打印Other Diagonal

q = [[1,X,X] [X,1,X] [X,1,X]] 什么都不做

q = [[X,1,X] [X,1,X] [X,1,X]] 应打印Horizontal

How the matrix should have its "winning conditions"

0 个答案:

没有答案