简体   繁体   English

为什么没有定义范围?

[英]Why isn't arange defined?

I have no programming experience and am learning Python for a class. 我没有编程经验,正在学习Python课程。 I'm going through conditionals now and have a problem that I'm not understanding the cause of. 我现在正在经历条件限制并遇到一个我不理解其原因的问题。

I'm basically just trying to let someone plot x^2 within a desired interval of x with the restriction that x isn't negative. 我基本上只是想让某人在x的期望间隔内绘制x ^ 2,其中x不是负的限制。 If the user inputs a negative input, they'll get a message saying it's not allowed. 如果用户输入负输入,他们将收到一条消息,说明不允许这样做。 Otherwise, x vs. x^2 should be plotted. 否则,应绘制x与x ^ 2。

N = input('Enter upper limit of x:');
if N < 0:
    print "Negative input isn't allowed."
else:
    x = arange(N)
    y = x*x
    plot(x,y) 

The above gives me the error: 以上给出了错误:

NameError: name 'arange' is not defined 

Which I don't understand because arange() is normally defined from my (very little) understanding. 我不明白,因为arange()通常是根据我的(非常小的)理解来定义的。

Python has a lot of built in commands. Python有很多内置命令。 However, arange and plot are not some of them. 然而, arangeplot是不是其中的一部分。 These are provided by numpy and matplotlib . 这些是由numpymatplotlib提供的。

To get your code to work, you could add: 要使代码正常工作,您可以添加:

from numpy import *
from matplotlib import *

at the top of your code. 在代码的顶部。 This isn't the recommended way though. 这不是推荐的方式。 It's better to do: 最好这样做:

import numpy
import matplotlib

and then when you call their functions 然后当你调用他们的功能

x=numpy.arange(N)

and

matplotlib.plot(x,y)

The reason that this is recommended is sometimes you may have other modules that have the same function names. 建议这样做的原因有时您可能有其他具有相同功能名称的模块。

A (very) slightly more advanced version that is even better is like: 一个(非常)稍微更高级的版本更好的是:

import numpy as np
import matplotlib.pyplot as plt
x=np.arange(N)
plt.plot(x,y)

np is a standard abbreviation for numpy and plt is a standard abbreviation of matplotlib.pyplot (which is where the command plot is actually defined). npnumpy的标准缩写, pltmatplotlib.pyplot的标准缩写(实际上是命令plot的定义)。 These are the standard abbreviations, and importing just part of matplotlib is good for reducing overhead. 这些是标准缩写,只导入matplotlib一部分有助于减少开销。

You should read up on what a numpy array is versus a usual python list. 你应该阅读numpy数组与普通python列表的对比。

Have you been exposed to python through the ipython interactive shell? 您是否通过ipython交互式shell暴露给python with ipython it is still possible to use the deprecated command line switch -pylab that exposes to the user the matplotlib interactive commands from the matplotlib.pyplot module and all the numpy library. 使用ipython ,仍然可以使用不推荐使用的命令行开关-pylab ,它向用户公开matplotlib.pyplot模块和所有numpy库中的matplotlib交互命令。

When you bring these habits in writing your first python program you will find that things are more complicated... 当你把这些习惯写成你的第一个python程序时,你会发现事情更复杂......

You have essentially two solutions, the first one is 你基本上有两个解决方案,第一个是

from pylab import *
...

that allows you to call unqualified function names like arange and plot from your script and, second one 这允许您从脚本中调用非限定函数名称,例如arangeplot ,第二个

import numpy as np
import matplotlib.pyplot as plt
# ...
x = np.arange(N)
# ...
plt.plot(x, x*x)

even if the second possibility is more verbose than the first, it is the recommended avenue: unqualified imports are considered bad practice because they pollute the namespace of your script, and this is particularly true with large modules like matplotlib.pyplot and numpy that defines hundreds of names! 即使第二种可能性比第一种更加冗长,也是建议的途径:不合格的导入被认为是不好的做法,因为它们污染了脚本的命名空间,对于像matplotlib.pyplotnumpy这样定义数百个模块的大型模块尤其如此名字!

Re the names used for the imports, that is np and plt , these choices are a sort of best practice agreement that you'd be wise to adopt, as you'll find on the net and on SO 1000's of examples that use exactly these names to access the plotting and numerical libraries. 重新使用用于导入的名称,即npplt ,这些选择是一种你应该明智采用的最佳实践协议,正如你在网上找到的那样,以及SO 1000的例子中使用的正是这些用于访问绘图和数值库的名称。

To summarize, using 总结一下,使用

import numpy as np
import matplotlib.pyplot as plt

is a so common idiom that it is what I advice you to do. 是一个如此常见的习语,我建议你这样做。

I think "arange" is from Numpy. 我认为“arange”来自Numpy。 You need to use "xrange" if you want the built in Python function. 如果你想要内置的Python函数,你需要使用“xrange”。 If you want the Numpy "arange", then you will need to import Numpy. 如果你想要Numpy“arange”,那么你需要导入Numpy。 Based on your snip of code, you are using Numpy so add: 基于你的代码片段,你正在使用Numpy,所以添加:

from numpy import *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM