以下代码中的错误是什么? (Python 2.7,矩阵)

时间:2016-08-24 07:45:34

标签: python-2.7 matrix

n= input("Enter size for matrix: ")
l=[[input("Enter number: ") for i in range(n)]for j in range(n)]
a=[[0 for i in range(n)]for j in range(n)]
for i in range(n):
    for j in range(n):
        a[j][i]=l[i][j]
for i in range(n):
    a[i]=a[i].reverse()
for i in range(n):
    for j in range(n):
        print a[i][j],
    print 

基本上,我想做的是将方形矩阵旋转90度。我想要的不是一个可以做同样的更有效的代码片段,而是这段代码中的错误。

引发NoneType错误。请修理。

2 个答案:

答案 0 :(得分:1)

如果你这样做:

$('#lineHeightInc')
.click(function() {
    var box = GetSelectedBox();
    var ct = box.data('LineHeight');
    if (isNaN(ct))
        ct = 0;
    ct++;
    box.css('line-height', (parseFloat(box.css('font-size')) * 1.61 + ct) + 'px');
    box.data('LineHeight', ct);
});

$('#lineHeightDic')
.click(function () {
    var box = GetSelectedBox();
    var ct = box.data('LineHeight');
    if (isNaN(ct))
        ct = 0;
    ct--;
    box.css('line-height', (parseFloat(box.css('font-size')) * 1.61 + ct) + 'px');
    box.data('LineHeight', ct);
});

a[i]=a[i].reverse() 得到a[i] None就地生效并直接影响reverse

只是做:

a[i]

BTW这种简化使我们得到一个很好的改变,以避免索引(和错误:)):

a[i].reverse()

答案 1 :(得分:0)

如果要将矩阵旋转90°

l = [

[1,2],

[3,4]

rotate_l = [

[3,1],

[4,2]

旋转矩阵代码:

n= input("Enter size for matrix: ")
l=[[input("Enter number: ") for i in range(n)]for j in range(n)]
a =[[0 for i in range(n)]for j in range(n)]
for i in range(n):
    for j in range(n):
        a[i][j] = l[n-j-1][i]
print a

如果你想转置矩阵

transposed_l = [

[1,3],

[2,4]

转置矩阵的代码:

n= input("Enter size for matrix: ")
l=[[input("Enter number: ") for i in range(n)]for j in range(n)]
a =[[0 for i in range(n)]for j in range(n)]
for i in range(n):
    for j in range(n):
        a[i][j] = l[j][i]
print a