更新时间:2022-10-15 15:27:35
您的输出已告诉您所有内容Quote:[0,0,0,0,1,2,0,2,4] / inner_list
[[0,0,0,0,1,2,0,2,4],[0,0,0,0,1,2,0,2,4],[0,0,0, 0,1,2,0,2,4]] / l
将inner_list
添加到l
,你只需添加一个指向inner_list
的指针,你就不要复制它。使用调试器,每次添加到inner_list
时,您都会看到l
增长。
----------
如果你不明白你的代码在做什么或为什么它做了什么,答案是调试器。
使用调试器查看代码正在执行的操作。它允许你逐行执行第1行并在执行时检查变量,它是一个令人难以置信的学习工具。
调试器 - ***,免费的百科全书 [ ^ ]
调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。
调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期效果时,你就接近一个错误。
[更新]引用:当我将'inner-list'附加到'l'时,最后为什么我得到3份inner_list作为''的最终值' l'
[0,0,0,0,1,2,0,2,4] / inner_list
[[0,0,0,0,1] ,2,0,2,4],[0,0,0,0,1,2,0,2,4],[0,0,0,0,1,2,0,2,4]] / l
为什么不
[[0,0,0],[0,0,0,0,1,2],[0,0,0] ,0,1,2,0,2,4]]作为'l'的最终值。这是Python的工作方式。
当你加入l
,你不复制列表,你添加一个活动的链接到l
。
请参阅数据结构 - Python 2.7.13文档 [ ^ ]。报价>Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i*j.
Note: i=0,1.., X-1; j=0,1,¡Y-1.
Example
Suppose the following inputs are given to the program:
3,5
Then, the output of the program should be:
[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
My program:x = int(raw_input()) y = int(raw_input()) l=[] for i in range(x): inner_list = [] for j in range(y): inner_list.append((i*j)) l.append(inner_list) print l
Instead of defining 'inner_list' inside the 'for i', if I define it outside as in:x = int(raw_input()) y = int(raw_input()) l=[] inner_list = [] for i in range(x): for j in range(y): inner_list.append((i*j)) l.append(inner_list)
I am not able to understand the output:
Output:
[0] /inner_list
[0, 0] /inner_list
[0, 0, 0] /inner_list
[[0, 0, 0]] /l
[0, 0, 0, 0] /inner_list
[0, 0, 0, 0, 1] /inner_list
[0, 0, 0, 0, 1, 2] /inner_list
[[0, 0, 0, 0, 1, 2], [0, 0, 0, 0, 1, 2]] /l
[0, 0, 0, 0, 1, 2, 0] /inner_list
[0, 0, 0, 0, 1, 2, 0, 2] /inner_list
[0, 0, 0, 0, 1, 2, 0, 2, 4] /inner_list
[[0, 0, 0, 0, 1, 2, 0, 2, 4], [0, 0, 0, 0, 1, 2, 0, 2, 4], [0, 0, 0, 0, 1, 2, 0, 2, 4]] /l
Why the final output is like above
[[0, 0, 0, 0, 1, 2, 0, 2, 4], [0, 0, 0, 0, 1, 2, 0, 2, 4], [0, 0, 0, 0, 1, 2, 0, 2, 4]] /l
and not
[[0,0,0],[0,0,0,0,1,2],[0,0,0,0,1,2,0,2,4]]
What I have tried:
I have tried to everything, but no luck.Your output already tells you everythingQuote:[0, 0, 0, 0, 1, 2, 0, 2, 4] /inner_list
[[0, 0, 0, 0, 1, 2, 0, 2, 4], [0, 0, 0, 0, 1, 2, 0, 2, 4], [0, 0, 0, 0, 1, 2, 0, 2, 4]] /l
When you addinner_list
tol
, you just add a pointer toinner_list
, you don't copy it. With the debugger, you would seel
grow every times you add toinner_list
.
----------
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.
Debugger - Wikipedia, the free encyclopedia[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
[Update]Quote:When I am appending the 'inner-list' to 'l', at the very end why I am getting 3 copies of inner_list as the final value of 'l'
[0, 0, 0, 0, 1, 2, 0, 2, 4] /inner_list
[[0, 0, 0, 0, 1, 2, 0, 2, 4], [0, 0, 0, 0, 1, 2, 0, 2, 4], [0, 0, 0, 0, 1, 2, 0, 2, 4]] /l
and why not
[[0, 0, 0],[0, 0, 0, 0, 1, 2],[0, 0, 0, 0, 1, 2, 0, 2, 4]] as the final value of 'l'.This is the way Python works.
When you add inl
, you don't copy the list, you add an active link tol
.
See Data Structures - Python 2.7.13 documentation[^].