int to str in list elements.. - Python

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

+ Reply to Thread
Results 1 to 7 of 7

int to str in list elements..

  1. Default 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


  2. Default 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))

  3. Default Re: int to str in list elements..

    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.


  4. Default Re: int to str in list elements..

    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

  5. Default Re: int to str in list elements..

    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.

  6. Default Re: int to str in list elements..

    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

  7. Default Re: int to str in list elements..


    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


+ Reply to Thread

Similar Threads

  1. FAQ 4.51 How do I permute N elements of a list?
    By Application Development in forum Perl
    Replies: 0
    Last Post: 12-04-2007, 03:03 AM
  2. Replies: 17
    Last Post: 10-18-2007, 01:45 PM
  3. remove list elements..
    By Application Development in forum Python
    Replies: 6
    Last Post: 10-05-2007, 10:54 AM
  4. list of elements testing
    By Application Development in forum Software-Testing
    Replies: 1
    Last Post: 10-27-2006, 07:29 AM
  5. Update list of elements
    By Application Development in forum XML SOAP
    Replies: 1
    Last Post: 04-26-2006, 01:24 AM