how many nested for can we utilize?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Aug 18 12:20:47 CEST 2008


En Sun, 17 Aug 2008 21:57:46 -0300, Patrol Sun <sunp1028 at gmail.com> escribió:

> Of course We needn't 100 levels,but I use the exec function can concise the
> code. See the attachment.

Just a note on the attached code:

def isPro52Num(n):
        s=[]
        for i in range(1,7):
                s.append(str(i*n))
        for i in s[0]:
                ll = len(string.join(string.split(s[0],i),''))
                for j in range(1,6):
                        if ll!=len(string.join(string.split(s[j],i),'')):
                                return False
        return True

The functions in the string module have been deprecated ages ago in favor of the corresponding string methods.
In this expression: len(string.join(string.split(s[0],i),'')) if you want to get "the number of digits in s[0] that are not i" I think it's way faster (and more clear) to do len(s[0])-s[0].count(i) instead of creating two lists just to throw them away.

-- 
Gabriel Genellina




More information about the Python-list mailing list