Re: int to str in list elements..
John Machin [EMAIL PROTECTED] wrote: On Oct 15, 4:02 am, Abandoned [EMAIL PROTECTED] 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, [EMAIL PROTECTED] Providenza Boekelheide, Inc. -- http://mail.python.org/mailman/listinfo/python-list
Re: int to str in list elements..
On Tue, 16 Oct 2007 06:18:51 +, Tim Roberts wrote: John Machin [EMAIL PROTECTED] wrote: On Oct 15, 4:02 am, Abandoned [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: int to str in list elements..
Marc 'BlackJack' Rintsch wrote: On Tue, 16 Oct 2007 06:18:51 +, Tim Roberts wrote: John Machin [EMAIL PROTECTED] wrote: On Oct 15, 4:02 am, Abandoned [EMAIL PROTECTED] 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] 301 x 8 = 2408 090 x 7 = 0630 009 x 6 = 0054 0009000 x 5 = 00045000 900 x 4 = 3600 090 x 3 = 0270 009 x 2 = 0018 --- 400 3096 less one for a comma counted above but not used - 3095 difference is 6 bytes which is one number (8) LESS 2 bytes Cheers, John -- http://mail.python.org/mailman/listinfo/python-list
Re: int to str in list elements..
Abandoned [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
int to str in list elements..
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: int to str in list elements..
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)) -- http://mail.python.org/mailman/listinfo/python-list
Re: int to str in list elements..
On Oct 15, 4:02 am, Abandoned [EMAIL PROTECTED] 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. -- http://mail.python.org/mailman/listinfo/python-list