extracting lists from a sequence

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mirage1987
    New Member
    • Jul 2007
    • 1

    extracting lists from a sequence

    [CODE=python]def dis(self):
    temp=[]
    list=[]
    for i in combilist:
    print list
    if i!=0:
    temp.append(i)
    else:
    print temp
    list.append(tem p)
    print list
    while temp:
    temp.pop()
    return[/CODE]

    combilist is....[1,0,2,0,3,4,5,0]
    list should comes out as [[1],[2],[3,4,5]]
    and temp is used for temporarily storage...

    but the output is comin out to be sumthin unexpected.....
    please tell me wat's the problem with the code...
    why is the list not appending correctly????
    Last edited by bartonc; Jul 5 '07, 08:58 AM. Reason: Added [CODE=python][CODE] tags.
  • dshimer
    Recognized Expert New Member
    • Dec 2006
    • 136

    #2
    This is meant to be more informational than fancy and will simply deal with the loop itself as you have written it.

    Code:
     >>> l=[]    #final list
    >>> t=[]    #temp list
    >>> for i in combilist:
    ...     if i!=0:
    ...         t.append(i)
    ...     elif t:    # only append temp list if there is something in it
    ...         l.append(t)
    ...         t=[]    # after it is appended, set it to empty
    ...         
    >>> print l
    [[1], [2], [3, 4, 5]]

    Comment

    • elbin
      New Member
      • Jul 2007
      • 27

      #3
      Originally posted by dshimer
      This is meant to be more informational than fancy and will simply deal with the loop itself as you have written it.

      Code:
       >>> l=[]    #final list
      >>> t=[]    #temp list
      >>> for i in combilist:
      ...     if i!=0:
      ...         t.append(i)
      ...     elif t:    # only append temp list if there is something in it
      ...         l.append(t)
      ...         t=[]    # after it is appended, set it to empty
      ...         
      >>> print l
      [[1], [2], [3, 4, 5]]
      There is only one problem that may arise from that, and it is that you lose the last list of numbers if combilist does not end with 0. You can just add it if you are not sure if it will be there all the time:

      Code:
      if combilist[-1] != 0:
          combilist.append(0)
      and then the rest of the code...
      That's the simplest method I could think of around the problem :)

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by elbin
        There is only one problem that may arise from that, and it is that you lose the last list of numbers if combilist does not end with 0. You can just add it if you are not sure if it will be there all the time:

        Code:
        if combilist[-1] != 0:
            combilist.append(0)
        and then the rest of the code...
        That's the simplest method I could think of around the problem :)
        Good point. You can also add an if statement after the for loop. Add another conditional to allow for the possibility of combilist starting with a 0:[code=Python]>>> combilist = [0,0,1,0,2,0,3,4 ,5,0,6,7,8]
        >>> lst = []
        >>> tem = []
        >>> for i in combilist:
        ... if i != 0:
        ... tem.append(i)
        ... else:
        ... if len(tem):
        ... lst.append(tem)
        ... tem = []
        ...
        >>> if i != 0:
        ... lst.append(tem)
        ...
        >>> print lst
        [[1], [2], [3, 4, 5], [6, 7, 8]]
        >>> [/code]

        Comment

        Working...