I'm going to do Matrix Addition on Python.(Not finish). But it shows an error.
我将在Python上做矩阵加法。(没有完成)。但它显示了一个错误。
m, n = (int(i) for i in raw_input().split())
a = [[0 for i in range(m)] for j in range(n)]
b = [[0 for i in range(m)] for j in range(n)]
c = []
total = []
for i in range(m):
x = raw_input()
for j in range(n):
value = [int(i) for i in x.split()]
c[i][j] = a[i][j]
#c.append(value)
print a
for i in c:
print i
I want to input
我想输入
3 3 <-- matrix dimensional m*n
3 3 <——矩阵维m*n。
1 2 3 >
1 2 3 >
3 2 1 > matrix A
3 2 1 >矩阵A。
1 3 2 >
1 2 3 >
1 1 1 >
1 1 1 >
1 1 1 > matrix B
1 >矩阵B。
1 1 1 >
1 1 1 >
and shows as
和显示
2 3 4 >
2 3 4 >
4 3 2 > matrix A + B
4 3 2 >矩阵A + B。
2 4 3 >
2 4 3 >
5 个解决方案
#1
7
You are using i
in your outer for
loop, and it is an int. Then in the loop you have:
你在你的外部循环中使用i,它是一个整数,然后在循环中你有:
value = [int(i) for i in x.split()]
which makes i
a string (which is what split
returns). Maybe you think there is some sort of scoping inside [ ]
? There isn't. You have a name collision, change one of them.
它使我成为一个字符串(这就是split返回)。也许你认为[]里面有某种范围的范围?没有。你有一个名字碰撞,改变其中一个。
#2
1
You are using same variable in inner for loop.
在内部循环中使用相同的变量。
for i in range(m):
x = raw_input()
for j in range(n):
# variable i is refering to outer loop
value = [int(p) for p in x.split()]
c[i][j] = a[i][j]
#c.append(value)
print a
for i in c:
print i
#3
1
Beyond the first two answers you'll have a problem with this statement:
除了前两个答案,你会有一个问题:
c[i][j] = a[i][j]
When the loop starts i
will be 0 and that's so far OK, but c
is an empty list and has no iterable at the first position so c[0][0]
will return an error. Get rid of it and uncomment the following line:
当循环开始时,我将是0,到目前为止还可以,但是c是一个空列表,在第一个位置上没有迭代,所以c[0][0]将返回一个错误。去掉它,取消注释:
#c.append(value)
EDIT:
编辑:
Your code won't return what you want. You'd better make something like this to create a matrix with the given sides:
你的代码不会返回你想要的。你最好做一些这样的事情来创建一个矩阵,它有一个给定的边:
for i in range(m):
d = []
for j in range(n):
x = raw_input()
d.append(int(x))
c.append(d)
If you have 3 for both m
and n
, then you will create matrix with sides 3 x 3 saved in the variable c
. In this way you don't have to split the user input. The user can give a number at a time. And you could even change the following line:
如果你对m和n都有3,那么你就会创建矩阵,在变量c中保存3 x 3,这样你就不需要分割用户输入。用户可以一次给出一个数字。你甚至可以改变下一行:
x = raw_input()
to:
:
x = raw_input("{0}. row, {1}. column: ".format(i+1, j+1))
Try it out!
试一下!
#4
0
import time
m, n = (int(i) for i in raw_input().split())
a = []
b = []
total = [[0 for i in range(n)] for j in range(m)]
for i in range(m):
x = raw_input()
for j in range(n):
value = [int(i) for i in x.split()]
a.append(value)
#print a
for i in range(m):
x = raw_input()
for j in range(n):
value = [int(i) for i in x.split()]
b.append(value)
#print b
for i in range(m):
for j in range(n):
total[i][j] = a[i][j] + b[i][j]
for i in total:
print ' '.join(map(str, i))
time.sleep(2)
OK! I just figured it out! Thank you
好的!我刚想出来了!谢谢你!
#5
0
you can also hit this error if you declare an int and treat it like a dict
如果您声明一个int并将其视为一个命令,您也可以点击这个错误。
>>> a = []
>>> a['foo'] = 'bar'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not str
#1
7
You are using i
in your outer for
loop, and it is an int. Then in the loop you have:
你在你的外部循环中使用i,它是一个整数,然后在循环中你有:
value = [int(i) for i in x.split()]
which makes i
a string (which is what split
returns). Maybe you think there is some sort of scoping inside [ ]
? There isn't. You have a name collision, change one of them.
它使我成为一个字符串(这就是split返回)。也许你认为[]里面有某种范围的范围?没有。你有一个名字碰撞,改变其中一个。
#2
1
You are using same variable in inner for loop.
在内部循环中使用相同的变量。
for i in range(m):
x = raw_input()
for j in range(n):
# variable i is refering to outer loop
value = [int(p) for p in x.split()]
c[i][j] = a[i][j]
#c.append(value)
print a
for i in c:
print i
#3
1
Beyond the first two answers you'll have a problem with this statement:
除了前两个答案,你会有一个问题:
c[i][j] = a[i][j]
When the loop starts i
will be 0 and that's so far OK, but c
is an empty list and has no iterable at the first position so c[0][0]
will return an error. Get rid of it and uncomment the following line:
当循环开始时,我将是0,到目前为止还可以,但是c是一个空列表,在第一个位置上没有迭代,所以c[0][0]将返回一个错误。去掉它,取消注释:
#c.append(value)
EDIT:
编辑:
Your code won't return what you want. You'd better make something like this to create a matrix with the given sides:
你的代码不会返回你想要的。你最好做一些这样的事情来创建一个矩阵,它有一个给定的边:
for i in range(m):
d = []
for j in range(n):
x = raw_input()
d.append(int(x))
c.append(d)
If you have 3 for both m
and n
, then you will create matrix with sides 3 x 3 saved in the variable c
. In this way you don't have to split the user input. The user can give a number at a time. And you could even change the following line:
如果你对m和n都有3,那么你就会创建矩阵,在变量c中保存3 x 3,这样你就不需要分割用户输入。用户可以一次给出一个数字。你甚至可以改变下一行:
x = raw_input()
to:
:
x = raw_input("{0}. row, {1}. column: ".format(i+1, j+1))
Try it out!
试一下!
#4
0
import time
m, n = (int(i) for i in raw_input().split())
a = []
b = []
total = [[0 for i in range(n)] for j in range(m)]
for i in range(m):
x = raw_input()
for j in range(n):
value = [int(i) for i in x.split()]
a.append(value)
#print a
for i in range(m):
x = raw_input()
for j in range(n):
value = [int(i) for i in x.split()]
b.append(value)
#print b
for i in range(m):
for j in range(n):
total[i][j] = a[i][j] + b[i][j]
for i in total:
print ' '.join(map(str, i))
time.sleep(2)
OK! I just figured it out! Thank you
好的!我刚想出来了!谢谢你!
#5
0
you can also hit this error if you declare an int and treat it like a dict
如果您声明一个int并将其视为一个命令,您也可以点击这个错误。
>>> a = []
>>> a['foo'] = 'bar'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not str