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

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

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

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

>>> 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
>>>

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

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

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

> 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

> 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

> 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

 Page 1 of 1 [ 10 post ]

Relevant Pages