fast way of turning a list of integers<256 into a string 
Author Message
 fast way of turning a list of integers<256 into a string

s=''
for i in range(1,255):
  s+=chr(i)

has horrible performance (primarily due to string
immutability I think).  Is there a function to do
the ff. instead:

x=range(1,255)
list2str(x)

and have x become a string?



Sat, 02 Jul 2005 06:44:51 GMT  
 fast way of turning a list of integers<256 into a string

Quote:

> s=''
> for i in range(1,255):
>   s+=chr(i)

> has horrible performance (primarily due to string
> immutability I think).  Is there a function to do
> the ff. instead:

> x=range(1,255)
> list2str(x)

What about this?

 >>> import struct
 >>> struct.pack('BB', *(254, 255))
'\xfe\xff'
 >>>

Or applied to your example:

 >>> x = range(1, 255)
 >>> fmt = 'B' * len(x)
 >>> struct.pack(fmt, *x)
'\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f
jklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe'
 >>>

cg.



Sat, 02 Jul 2005 07:00:10 GMT  
 fast way of turning a list of integers<256 into a string

Quote:

> s=''
> for i in range(1,255):
>   s+=chr(i)

> has horrible performance (primarily due to string
> immutability I think).  Is there a function to do
> the ff. instead:

> x=range(1,255)
> list2str(x)

How about this?

Quote:
>>> x = range(65,91)
>>> ''.join(map(chr, x))

'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

-Andrew.



Sat, 02 Jul 2005 07:07:21 GMT  
 fast way of turning a list of integers<256 into a string

Quote:
> s=''
> for i in range(1,255):
>   s+=chr(i)

> has horrible performance (primarily due to string
> immutability I think).  Is there a function to do
> the ff. instead:

> x=range(1,255)
> list2str(x)

> and have x become a string?

Use map or list comprehensions to convert all the elements using chr
and then use ''.join on the list.

x = range(1,256)
x = map(chr, x)
x = ''.join(x)

or in one step:

x = ''.join(map(chr, range(1, 256)))

HTH,
Dave



Sat, 02 Jul 2005 07:11:40 GMT  
 fast way of turning a list of integers<256 into a string
--On Monday, January 13, 2003 14:44:51 -0800 "Jonathan P."

Quote:

> s=''
> for i in range(1,255):
>   s+=chr(i)

> has horrible performance (primarily due to string
> immutability I think).  Is there a function to do
> the ff. instead:

> x=range(1,255)
> list2str(x)

> and have x become a string?
> --
> http://mail.python.org/mailman/listinfo/python-list

''.join(map(chr, range(1,255))) is pretty fast, as is

import array
array.array('B', range(1,255)).tostring()

I haven't profiled to see which is faster.

Andrew



Sat, 02 Jul 2005 07:00:22 GMT  
 fast way of turning a list of integers<256 into a string

Quote:

> s=''
> for i in range(1,255):
>   s+=chr(i)

> has horrible performance (primarily due to string
> immutability I think).  Is there a function to do
> the ff. instead:

> x=range(1,255)
> list2str(x)

> and have x become a string?

Hi
everybody has one shot free.
Here is mine one:
Quote:
>>> text=reduce(lambda x,y:x+chr(y),range(32,128,1),'')
>>> text

jklmnopqrstuvwxyz{|}~\x7f'
regards
Peter


Sat, 02 Jul 2005 15:40:18 GMT  
 fast way of turning a list of integers<256 into a string
I would try something along the lines of

import array
array.array('B',range(1,255)).tostring()

Quote:
-----Original Message-----

On Behalf Of Peter Abel
Sent: 14 January 2003 07:40

Subject: Re: fast way of turning a list of integers<256 into a string



> s=''
> for i in range(1,255):
>   s+=chr(i)

> has horrible performance (primarily due to string immutability I
> think).  Is there a function to do the ff. instead:

> x=range(1,255)
> list2str(x)

> and have x become a string?

Hi
everybody has one shot free.
Here is mine one:
>>> text=reduce(lambda x,y:x+chr(y),range(32,128,1),'')
>>> text
'

ghijklmnopqrstuvwxyz{|}~\x7f'

regards
Peter
--
http://mail.python.org/mailman/listinfo/python-list



Sat, 02 Jul 2005 16:52:29 GMT  
 fast way of turning a list of integers<256 into a string
Well, if we all get an entry free, here's my primary entry...

 >>> d = "".join( map(str,myints) )

Would be my entry for small arrays, it's straightforward and
idio(t)matic :) .

 >>> struct.pack( ">"*len(myints), *myints )

is slightly faster than the "".join version for 100,000 items on my
machine (there's fewer individually-managed objects created), but it's
less idiomatic IMO, so I won't put it in as my entry ;) .  If there are
large arrays involved you should obviously import Numeric and do it
right :) .

Enjoy,
Mike

...

Quote:
>Hi
>everybody has one shot free.
>Here is mine one:

>>>>text=reduce(lambda x,y:x+chr(y),range(32,128,1),'')
>>>>text

jklmnopqrstuvwxyz{|}~\x7f'

>regards
>Peter

_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/


Sat, 02 Jul 2005 17:02:09 GMT  
 fast way of turning a list of integers<256 into a string
That second non-entry should have been:

 >>> d = struct.pack( 'B'*len(myints), *myints )

Forgot to copy back the fixed version from the interpreter :) .

Sigh,
Mike

...

Quote:
> >>> struct.pack( ">"*len(myints), *myints )

...


Sat, 02 Jul 2005 17:16:59 GMT  
 fast way of turning a list of integers<256 into a string

Quote:

> Is there a function to do the ff. instead:

> x=range(1,255)
> list2str(x)

> and have x become a string?

Bizarrely enough:

    http://www.python.org/doc/essays/list2str.html

A touch out of date now, but still broadly accurate.

Cheers,
M.

--
  surely, somewhere, somehow, in the history of computing, at least
  one manual has been written that you could at least remotely
  attempt to consider possibly glancing at.              -- Adam Rixey



Mon, 04 Jul 2005 03:08:05 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Problems with picture (256 color) on desktop with colorsettings 256 colors

2. Integer and String Dictionary Keys for Fast Access

3. strings turning to lists ??

4. ADVICE <<<<<<<<<<<<<<<<<<<-----------EASY ---------------

5. HELP: shimmering integer <> string

6. Turning a list value in a parameter into a list for eval without quasiquote/unquote

7. I SEARCH ASSEMBLER CODERS <<<<<<<<<<<<

8. Integer <-> string and Floating point <-> string conversion

9. Q: ways to convert floats to integers (fixnums)

10. DBConnection<<open connection string size limit...

11. as(<string>, <type>)

 

 
Powered by phpBB® Forum Software