[英]cannot print the list “int” object not iterable
def createOneRow(width):
""" returns one row of zeros of width "width"...
You should use this in your
createBoard(width, height) function """
row = []
for col in range(width):
row += [0]
return row
def createBoard(width,height):
"""creates a list
"""
row = []
for col in range(height):
row += createOneRow(width),
return row
import sys
def printBoard(A):
""" this function prints the 2d list-of-lists
A without spaces (using sys.stdout.write)
"""
for row in A:
for col in row:
sys.stdout.write(str(col))
sys.stdout.write('\n')
Above is the basic function, then I am asked to do a copy function to keep track of the original A
. 以上是基本功能,然后要求我执行复制功能以跟踪原始
A
def copy(A):
height=len(A)
width=len(A[0])
newA=[]
row=[]
for row in range(0,height):
for col in range(0,width):
if A[row][col]==0:
newA+=[0]
elif A[row][col]==1:
newA+=[1]
return newA
Then I tried to printBoard(newA)
and here comes the error: 然后我尝试了
printBoard(newA)
并且出现了错误:
Traceback (most recent call last):
File "<pyshell#37>", line 1, in <module>
printBoard(newA)
File "/Users/amandayin/Downloads/wk7pr2/hw7pr2.py", line 35, in printBoard
for col in row:
TypeError: 'int' object is not utterable
Can someone please tell me why this is an error? 有人可以告诉我为什么这是一个错误吗?
Here is my solution, which I tested: 这是我测试过的解决方案:
def copy(a):
return [row[:] for row in a]
If this is not homework, use copy.deepcopy()
: 如果这不是家庭作业,请使用
copy.deepcopy()
:
import copy
b = copy.deepcopy(a)
Your function copy
is incorrect. 您的函数
copy
不正确。 This code: 这段代码:
def copy(A):
height=len(A)
width=len(A[0])
newA=[]
row=[]
for row in range(0,height):
for col in range(0,width):
if A[row][col]==0:
newA+=[0]
elif A[row][col]==1:
newA+=[1]
return newA
a = [
[1, 1, 0],
[0, 1, 1],
]
print a
print copy(a)
prints this: 打印此:
[[1, 1, 0], [0, 1, 1]]
[1, 1, 0, 0, 1, 1]
As you see it doesn't contain sublists, so it tries to iterate integers. 如您所见,它不包含子列表,因此它尝试迭代整数。
I thing using copy.deepcopy
would do the job. 我使用
copy.deepcopy
可以完成这项工作。
You aren't copying the list correctly, I think. 我认为您没有正确复制列表。
Your original list looks something like this: 您的原始列表如下所示:
[[1,2,3],[4,5,6],[7,8,9]]
When you copy it, you create a new list called newA: 复制它时,您将创建一个名为newA的新列表:
[]
and you just add elements to it: 您只需向其中添加元素:
[1,2,3,4,5,6,7,8,9]
so your list format is different. 因此您的列表格式是不同的。
This is perhaps what you intended: 这可能是您想要的:
newA=[]
row=[]
for row in range(0,height):
newRow = []
for col in range(0,width):
if A[row][col]==0:
newRow+=[0]
elif A[row][col]==1:
newRow+=[1]
newA += [newRow]
You have not provided enough code to reproduce the problem, but I can tell you what that error means. 您没有提供足够的代码来重现该问题,但是我可以告诉您该错误的含义。 It means that, in the expression
for col in row:
, row
is actually an int
rather than some kind of iterable
type. 这意味着,
for col in row:
表达式for col in row:
, row
实际上是一个int
而不是某种iterable
类型。
Tried running this and noticed that there is a comma at the end of increment statement in CreateBoard: 尝试运行此命令,并发现CreateBoard中的增量语句末尾有一个逗号:
row += createOneRow(width),
This is missing in CopyA method when incrementing newA. 当增加newA时,在CopyA方法中缺少此功能。 I tried adding a comma to these lines and there is no error.
我尝试在这些行中添加逗号,并且没有错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.