Python(试图理解流程) [英] Python (trying to understand the flow)

查看:72
本文介绍了Python(试图理解流程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个程序,它取2位数,X,Y作为输入,并生成一个二维数组。数组的第i行和第j列中的元素值应为i * j。

注意:i = 0,1 ..,X-1; j = 0,1,¡Y-1。

示例

假设以下输入被赋予程序:

3,5

然后,程序的输出应该是:

[[0,0,0,0,0],[0,1,2,3,4], [0,2,4,6,8]]



我的节目:

 x = int(raw_input())
y = int(raw_input())

l = []
for i 范围(x):
inner_list = []
for j 范围内(y):
inner_list.append((i * j))
l.append(inner_list)
print l







而不是在'for i'中定义'inner_list',如果我在外面定义它:

 x = int(raw_input())
y = int(raw_input())

l = []
inner_list = []
for i <小号范围内的pan class =code-keyword>(x):

j in 范围(y):
inner_list.append((i * j))
l.append(inner_list)





我无法理解输出:



输出:

[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



W hy最终输出如上所述

[[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]]



我的尝试:



我已尝试过一切,但没有运气。

解决方案

您的输出已告诉您所有内容

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 everything

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


When you add inner_list to l, you just add a pointer to inner_list, you don't copy it. With the debugger, you would see l grow every times you add to inner_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 in l, you don't copy the list, you add an active link to l.


See Data Structures - Python 2.7.13 documentation[^].


这篇关于Python(试图理解流程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆