counting occurances in lists within lists.

Anton Vredegoor anton at vredegoor.doge.nl
Sat Dec 21 14:15:04 EST 2002


On Sat, 21 Dec 2002 13:43:26 -0500, Peter Hansen <peter at engcorp.com>
wrote:

>It's always nice to get the answer yourself... but this is 
>comp.lang.python so a long thread with variations on the theme
>is mandatory.

Absolutely true. Especially since your solution doesn't use pythons
latest flattening algo :-)

Regards,
	Anton.

def flatten(L):
    res = []
    R = L[:]
    while R:
        x = R.pop(0)
        if type(x) == list:
            R = x + R
        else:
            res.append(x)
    return res
            
def test():
    L = [[0,0,0,1,2,3], [4,5,6,7,8,9], [10,11,12,13,14,15]]
    print flatten(L).count(0)

if __name__=='__main__':
    test()




More information about the Python-list mailing list