list to tuple

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zxo102

    #1

    list to tuple

    Hi,
    I got several dynamic lists a1, b1, c1, .... from a python
    application such as
    a1 = [1,5,3,2,5,...], the len(a1) varies. Same to b1, c1, ....

    With python, I would like to reorganize them into a tuple like

    t1 = ((a1[0],b1[0],c1[0],...),(a1[1],b1[1],c1[1],...),...)

    Anybody knows how to do that. Thanks for your help.

    Ouyang

  • James Stroud

    #2
    Re: list to tuple


    Try the zip funciton:

    py> a = [11,12,13,14]
    py> b = [2,3,4,5]
    py> c = [20,21,22,23,24, 25]

    py> zip(a,b,c)
    [(11, 2, 20), (12, 3, 21), (13, 4, 22), (14, 5, 23)]



    On Thursday 11 August 2005 09:05 pm, zxo102 wrote:[color=blue]
    > Hi,
    > I got several dynamic lists a1, b1, c1, .... from a python
    > application such as
    > a1 = [1,5,3,2,5,...], the len(a1) varies. Same to b1, c1, ....
    >
    > With python, I would like to reorganize them into a tuple like
    >
    > t1 = ((a1[0],b1[0],c1[0],...),(a1[1],b1[1],c1[1],...),...)
    >
    > Anybody knows how to do that. Thanks for your help.
    >
    > Ouyang[/color]



    --
    James Stroud
    UCLA-DOE Institute for Genomics and Proteomics
    Box 951570
    Los Angeles, CA 90095


    Comment

    • Ruslan Spivak

      #3
      Re: list to tuple

      "zxo102" <zxo102@gmail.c om> writes:
      [color=blue]
      > Hi,
      > I got several dynamic lists a1, b1, c1, .... from a python
      > application such as
      > a1 = [1,5,3,2,5,...], the len(a1) varies. Same to b1, c1, ....
      >
      > With python, I would like to reorganize them into a tuple like
      >
      > t1 = ((a1[0],b1[0],c1[0],...),(a1[1],b1[1],c1[1],...),...)
      >
      > Anybody knows how to do that. Thanks for your help.
      >[/color]

      t1 = tuple(zip(a1, b1, c1))

      I don't know your requirements, so consider also izip from itertools.

      Ruslan

      Comment

      • Paddy

        #4
        Re: list to tuple

        Try this:
        [color=blue][color=green][color=darkred]
        >>> a,b,c = list('tab'),lis t('era'),list(' net')
        >>> a,b,c[/color][/color][/color]
        (['t', 'a', 'b'], ['e', 'r', 'a'], ['n', 'e', 't'])[color=blue][color=green][color=darkred]
        >>> tuple(((x,y,z) for x,y,z in zip(a,b,c)))[/color][/color][/color]
        (('t', 'e', 'n'), ('a', 'r', 'e'), ('b', 'a', 't'))[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        - Paddy.

        Comment

        • zxo102

          #5
          Re: list to tuple

          Thanks for your help.

          Comment

          Working...