开发者

Are there any fundamental differences to use numpy.zeros or zeros?

开发者 https://www.devze.com 2023-04-04 22:56 出处:网络
I have downloaded the python 2.6 and installed numpy-1.6.1-win32-superpack-python2.6 and scipy-0.9.0-win32-superpack-python2.6.It is running on a Window with window 2000 professional as OS.

I have downloaded the python 2.6 and installed numpy-1.6.1-win32-superpack-python2.6 and scipy-0.9.0-win32-superpack-python2.6. It is running on a Window with window 2000 professional as OS.

However, when I run python, with the following commands, following error message appear, could you mind to teach me how to solve it?

>>> x =开发者_高级运维 zeros([K], int32)

Traceback (most recent call last):
file "<stdin>", line 1, in <module>
NameError: name 'zeros' is not defined.

I then tried to import numpy:

>>> import numpy
>>> x=numpy.zeros([K], int32)
Traceback (most recent call last):
file "<stdin>", line 1, in <module>
NameError: name 'K' is not defined.

Are there any fundamental differences to use numpy.zeros or zeros? What is the difference between them?

I also tried to readin a series of files (saved in the same directory) to get python doing analysis for me. I learnt from the manual that I should use

f=open('C:/xxx.txt', 'w') # for single file

How to apply this to a series of files?


The error message says it all: You're using a name that hasn't been defined yet.

If you import numpy, and numpy contains a zeros() function, then you must call it as numpy.zeros(). If you want to refer to zeros() directly, you can from numpy import zeros.

If you pass the variable K to a function, K must have been assigned to something before. What is K supposed to be in your example?

As for opening files, I don't think the manual says that. At least, it should be f = open(r'C:\xxx.txt', 'w').

To open more than one in a loop, you can

for filename in filelist:
    with open(filename, 'w') as outfile:
        # do something.
        # The with block ensures that the file will be closed after use

Also check out the glob module and os.walk().

All this is covered pretty well in the Python tutorial.


Question 1

Your problem is not with numpy, it is simply that you have not defined K.

Question 2

One simple way to enumerate files in a directory is glob.

from glob import glob
for filename in glob('*.txt'):
    print filename


You need to read Python Tutorial to get your answers. The first code sample doesn't work because you didn't import zeros. The second - because you don't have K variable.

0

精彩评论

暂无评论...
验证码 换一张
取 消