how to test for nesting inlist
Alex Martelli
aleaxit at yahoo.com
Mon Jan 22 15:20:47 EST 2001
"Patricia Hawkins" <phawkins at spamnotconnact.com> wrote in message
news:wkn1cjjrb5.fsf at mail.connact.com...
> >>>>> "AM" == Alex Martelli <aleaxit at yahoo.com> writes:
>
> AM> <cpsoct at my-deja.com> wrote in message
news:94h7pi$b86$1 at nnrp1.deja.com...
> >> What are list comprehensions? I have looked and looked and looked in
> >> books and on www.python.org and i see no mention of these. Is this
>
> AM> A list comprehension is a syntax form such as:
>
> AM> [<expression> for <var> in <sequence>]
>
> Like this:
>
> def howDeep2(x):
> if type(x) != type([]):
> return 0
> return 1 + reduce(max, [howDeep(y) for y in x])
Yes, that's an example I posted recently, but I fear in this context it
might
perhaps confuse (by adding reduce, a typical 'functional' construct; as well
as recursion; plus a little mis-spelling as it tries to recursively call
'howDeep' but calls itself 'howDeep2'... the latter, my fault!-).
There are many simpler examples, e.g, "return a list of N random numbers":
def randomN(N):
return [random.random() for i in range(N)]
or "return the squares of the integers from A to B included":
def squares(A,B):
return [x*x for x in range(A,B+1)]
etc, etc.
Alex
More information about the Python-list
mailing list