[Numpy-discussion] Numeric odd tostring() behaviour

Russell Valentine russ at coldstonelabs.org
Tue Oct 21 17:00:10 CDT 2003



I understand my error. Everyone who responded, you have my thanks.


Russell Valentine


On 21 Oct 2003 09:16:08 -0400
Todd Miller <jmiller at stsci.edu> 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"))
> > 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20031021/7c43a546/attachment-0001.bin 


More information about the Numpy-discussion mailing list