#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    15
    Rep Power
    0

    Type Check Problem


    Quick question...

    I want to add up elements in a list...pretty simple. But when some of those elements are nested lists, I want to add those up also. And then add the sum of the nested lists to the sum of the original list.

    For example:

    Code:
    nested_list = [3, 43, 1, [11, 15, 3, 23, 2], [6, 4, 1, 88], [12, 53, 96]]
    
    total = 0
    for nested in nested_list:
        if nested == type(list):
            x = sum(nlist)
            total += x
            print(total)
        else:
            total += nested
            print(total)
    When the code hits the first nested list I get the "unsupported operand type" error.
    Why is it skipping over the if and going to the else?
    Any help is much appreciated. Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    430
    Rep Power
    66
    Originally Posted by CastorTroy
    Code:
        if nested == type(list):
    When the code hits the first nested list I get the "unsupported operand type" error.
    A slight mistake. You meant:

    Code:
        if type(nested) == type(list):
    Actually, though, the recommended way is:

    Code:
        if isinstance(nested, list):
    In any case, your solution works only on two levels (there is no true recursion), but I guess thatís no problem?
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,840
    Rep Power
    480
    Someone recently mentioned lisp, thus
    Code:
    def car(L):
        return L[0]
    
    def cdr(L):
        return L[1:]
    
    def flatten(L):
        '''
            >>> flatten([3,4,[43,34,[23,2423]]])
            [3, 4, 43, 34, 23, 2423]
            >>> flatten(3)
            [3]
            >>> flatten([])
            []
        '''
        if isinstance(L,str):
            return [L]
        if hasattr(L,'__len__'):
            if 0 == len(L):
                return []
            return flatten(car(L)) + flatten(cdr(L))
        return [L]
    
    print(sum(flatten([3, 43, 1, [11, 15, 3, 23, 2], [6, 4, 1, 88], [12, 53, 96]])))
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    15
    Rep Power
    0
    Originally Posted by SuperOscar
    A slight mistake. You meant:

    Code:
        if type(nested) == type(list):
    Actually, though, the recommended way is:

    Code:
        if isinstance(nested, list):
    In any case, your solution works only on two levels (there is no true recursion), but I guess thatís no problem?
    That was perfect...many thanks

IMN logo majestic logo threadwatch logo seochat tools logo