1. Use a generator expression:
b = ",".join(str(i) for i in a)
or
2. Use imap
from itertools import imap
b = ",".join(imap(str, a))
This is a discussion on int to str in list elements.. - Python ; Hi.. I have a list as a=[1, 2, 3 .... ] (4 million elements) and b=",".join(a) than TypeError: sequence item 0: expected string, int found I want to change list to a=['1','2','3'] but i don't want to use FOR because ...
Hi..
I have a list as a=[1, 2, 3 .... ] (4 million elements)
and
b=",".join(a)
than
TypeError: sequence item 0: expected string, int found
I want to change list to a=['1','2','3'] but i don't want to use FOR
because my list very very big.
I'm sorry my bad english.
King regards
1. Use a generator expression:
b = ",".join(str(i) for i in a)
or
2. Use imap
from itertools import imap
b = ",".join(imap(str, a))
On Oct 15, 4:02 am, Abandoned <best...@gmail.com> wrote:
> Hi..
> I have a list as a=[1, 2, 3 .... ] (4 million elements)
> and
> b=",".join(a)
> than
> TypeError: sequence item 0: expected string, int found
> I want to change list to a=['1','2','3'] but i don't want to use FOR
> because my list very very big.
What is your worry: memory or time? The result string will be very
very very big. What will you do with the result string -- write it to
a file? If so, look at the cPickle module.
Abandoned <besturk@gmail.com> wrote:
> Hi..
> I have a list as a=[1, 2, 3 .... ] (4 million elements)
> and
> b=",".join(a)
> than
> TypeError: sequence item 0: expected string, int found
> I want to change list to a=['1','2','3'] but i don't want to use FOR
> because my list very very big.
> I'm sorry my bad english.
> King regards
Try b=','.join(map(str, a)) -- it WILL take up some memory (temporarily)
to build the huge resulting string, but there's no real way to avoid
that.
It does run a bit faster than a genexp with for...:
brain:~ alex$ python -mtimeit -s'a=range(4000*1000)'
'b=",".join(map(str,a))'
10 loops, best of 3: 3.37 sec per loop
brain:~ alex$ python -mtimeit -s'a=range(4000*1000)' 'b=",".join(str(x)
for x i
n a)'
10 loops, best of 3: 4.36 sec per loop
Alex
John Machin <sjmachin@lexicon.net> wrote:
>On Oct 15, 4:02 am, Abandoned <best...@gmail.com> wrote:
>> Hi..
>> I have a list as a=[1, 2, 3 .... ] (4 million elements)
>> and
>> b=",".join(a)
>> than
>> TypeError: sequence item 0: expected string, int found
>> I want to change list to a=['1','2','3'] but i don't want to use FOR
>> because my list very very big.
>
>What is your worry: memory or time? The result string will be very
>very very big.
It's an interesting mental exercise to try to figure out just how large
that string will be, without using Python.
I get 30,888,889 bytes...
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
On Tue, 16 Oct 2007 06:18:51 +0000, Tim Roberts wrote:
> John Machin <sjmachin@lexicon.net> wrote:
>>On Oct 15, 4:02 am, Abandoned <best...@gmail.com> wrote:
>>> Hi..
>>> I have a list as a=[1, 2, 3 .... ] (4 million elements)
>>> and
>>> b=",".join(a)
>>> than
>>> TypeError: sequence item 0: expected string, int found
>>> I want to change list to a=['1','2','3'] but i don't want to use FOR
>>> because my list very very big.
>>
>>What is your worry: memory or time? The result string will be very
>>very very big.
>
> It's an interesting mental exercise to try to figure out just how large
> that string will be, without using Python.
>
> I get 30,888,889 bytes...
I think you have an off by one error here. (One number, not one byte) :-)
Ciao,
Marc 'BlackJack' Rintsch
Marc 'BlackJack' Rintsch wrote:
> On Tue, 16 Oct 2007 06:18:51 +0000, Tim Roberts wrote:
>
> > John Machin <sjmachin@lexicon.net> wrote:
> >>On Oct 15, 4:02 am, Abandoned <best...@gmail.com> wrote:
> >>> Hi..
> >>> I have a list as a=[1, 2, 3 .... ] (4 million elements)
> >>> and
> >>> b=",".join(a)
> >>> than
> >>> TypeError: sequence item 0: expected string, int found
> >>> I want to change list to a=['1','2','3'] but i don't want to use FOR
> >>> because my list very very big.
> >>
> >>What is your worry: memory or time? The result string will be very
> >>very very big.
> >
> > It's an interesting mental exercise to try to figure out just how large
> > that string will be, without using Python.
> >
> > I get 30,888,889 bytes...
>
> I think you have an off by one error here. (One number, not one byte) :-)
It's certainly off :
[Best viewed in a fixed-width font ... umm, do they still sell squared
paper for doing arithmetic on? I had to rip a page out of a notebook
and rotate it through 90 degrees]
3000001 x 8 = 24000008
0900000 x 7 = 06300000
0090000 x 6 = 00540000
0009000 x 5 = 00045000
0000900 x 4 = 00003600
0000090 x 3 = 00000270
0000009 x 2 = 00000018
------- --------
4000000 30888896
less one for a comma counted above but not used -> 30888895
difference is 6 bytes which is one number (8) LESS 2 bytes
Cheers,
John