From: Russell V. <ru...@co...> - 2003-10-21 05:29:06
Attachments:
NumericTest.py
|
Hello, It may be my fault, but I think the following behaviour is odd. If I try to change a array to a string it seems like it adds a lot of extra zero characters. Take the following script attached as a example, it gives me this output. ta.tostring is not equal Zero characters - 25 255 characters - 3 tast is equal Zero characters - 4 255 characters - 3 tostring() is so much more faster than the second way, but it isn't giving me the desired results. Have I done something wrong? I'm using Numeric 23.1 Thanks for your help. Russell Valentine |
From: Todd M. <jm...@st...> - 2003-10-21 16:58:49
|
I think the problem is that the default integer precision is most likely 32-bits, and you appear to be assuming it will be 8-bits. If you declare your array using typecode=Numeric.UInt8 as an extra parameter, you will force the type to match your assumption and things will work out as you expect. Regards, Todd On Tue, 2003-10-21 at 00:28, Russell Valentine wrote: > Hello, > > It may be my fault, but I think the following behaviour is odd. If I > try to change a array to a string it seems like it adds a lot of extra > zero characters. Take the following script attached as a example, it gives > me this output. > > ta.tostring is not equal > Zero characters - 25 > 255 characters - 3 > tast is equal > Zero characters - 4 > 255 characters - 3 > > tostring() is so much more faster than the second way, but it isn't giving > me the desired results. Have I done something wrong? I'm using Numeric > 23.1 > > Thanks for your help. > > > Russell Valentine > ---- > > #!/bin/env python > > import string > import Numeric > > > ta = Numeric.array([0, 255, 255, 255,0,0,0]) > compare_string = "\x00\xff\xff\xff\x00\x00\x00" > if ta.tostring() == compare_string: > print "ta.tostring is equal" > else: > print "ta.tostring is not equal" > > print "Zero characters - "+str(ta.tostring().count("\x00")) > print "255 characters - "+str(ta.tostring().count("\xff")) > > tast = "" > for value in ta: > tast += chr(value) > > if tast == compare_string: > print "tast is equal" > else: > print "tast is not equal" > > print "Zero characters - "+str(tast.count("\x00")) > print "255 characters - "+str(tast.count("\xff")) > -- Todd Miller Space Telescope Science Institute 3700 San Martin Drive Baltimore MD, 21030 (410) 338 - 4576 |
From: Russell V. <ru...@co...> - 2003-10-21 23:59:38
|
I understand my error. Everyone who responded, you have my thanks. Russell Valentine On 21 Oct 2003 09:16:08 -0400 Todd Miller <jm...@st...> wrote: > I think the problem is that the default integer precision is most likely > 32-bits, and you appear to be assuming it will be 8-bits. If you > declare your array using typecode=Numeric.UInt8 as an extra parameter, > you will force the type to match your assumption and things will work > out as you expect. > > Regards, > Todd > > On Tue, 2003-10-21 at 00:28, Russell Valentine wrote: > > Hello, > > > > It may be my fault, but I think the following behaviour is odd. If > > I > > try to change a array to a string it seems like it adds a lot of extra > > zero characters. Take the following script attached as a example, it > > gives me this output. > > > > ta.tostring is not equal > > Zero characters - 25 > > 255 characters - 3 > > tast is equal > > Zero characters - 4 > > 255 characters - 3 > > > > tostring() is so much more faster than the second way, but it isn't > > giving me the desired results. Have I done something wrong? I'm using > > Numeric 23.1 > > > > Thanks for your help. > > > > > > Russell Valentine > > ---- > > > > > #!/bin/env python > > > > import string > > import Numeric > > > > > > ta = Numeric.array([0, 255, 255, 255,0,0,0]) > > compare_string = "\x00\xff\xff\xff\x00\x00\x00" > > if ta.tostring() == compare_string: > > print "ta.tostring is equal" > > else: > > print "ta.tostring is not equal" > > > > print "Zero characters - "+str(ta.tostring().count("\x00")) > > print "255 characters - "+str(ta.tostring().count("\xff")) > > > > tast = "" > > for value in ta: > > tast += chr(value) > > > > if tast == compare_string: > > print "tast is equal" > > else: > > print "tast is not equal" > > > > print "Zero characters - "+str(tast.count("\x00")) > > print "255 characters - "+str(tast.count("\xff")) > > |
From: Chris B. <Chr...@no...> - 2003-10-21 22:08:45
|
Russell Valentine wrote: > It may be my fault, but I think the following behaviour is odd. If I > try to change a array to a string it seems like it adds a lot of extra > zero characters. Take the following script attached as a example, it gives > me this output. You've misunderstood what tostring() is supposed to do. It takes the BINARY data representing the array, and dumps it into a Python string as a string of bytes. It is NOT doing a chr() on each element. As an example: >>> a = array((1.0)) >>> # a is now a one element Python Float (C double, 8 bytes) >>> s = a.tostring() >>> len(s) 8 >>> print repr(s) '\x00\x00\x00\x00\x00\x00\xf0?' >>> And there are your 8 bytes. By the way, in your code: tast = "" for value in ta: tast += chr(value) you could probably speed it up quite a bit by doing this instead: tast = [] for value in ta: tast.append(value) tast = "".join(tast) Using += with a string creates a new string, one charactor longer, each time. Appending to an a list is much faster, and the string join method is pretty quick also. -- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chr...@no... |