[Tutor] A very simple question

Gregor Lingl glingl@aon.at
Thu, 30 May 2002 21:28:46 +0200


Normally i check replies already posted, but for
some (not well understood) reason this time I give
an immediate reply. So expect some (if not only)
repetition:


# only lower strings (to Nicole  Seitz)

# if we put all the ugly things we have to check
# into a special separate function:

def good(strg):
    return type(strg)==type("") and strg!="" and strg[0].islower()

# the classical solution to your problem might look somewhat
# like this:

def checkIfOnlyLower1(lst):
    ok = 1
    for item in lst:
        ok = ok and good(item)
    return ok

# if you like to use the function map, whick outputs
# a list of results when applying some given function
# (in our case: map) to all the elements of a given
# list, you may arrive at:

def checkIfOnlyLower2(lst):
    return 0 not in map(good, lst)

# if, moreover you like to use reduce and 'operators as
# functions' this is an alternative:

def checkIfOnlyLower3(lst):
    from operator import and_
    return reduce(and_, map(good, lst))

# -- if you are sure, that ther are only nonempty strings in
# -- your list and you moreover like lambda instead of extra
# -- functions. you could do it with this single function:

def checkIfOnlyLower4(lst):
    from operator import and_
    return reduce(and_, map(lambda x: x.islower(), lst))

# ----------------------------------------------------------

So there you have some starting point for the study of
functional programming (if you like)

Best wishes
Gregor



----- Original Message -----
From: "Nicole Seitz" <Nicole.Seitz@urz.uni-hd.de>
To: <tutor@python.org>
Sent: Thursday, May 30, 2002 6:34 PM
Subject: [Tutor] A very simple question


Hi there!

I'd like to write a function that gets a list , checks if there are only
strings in the list starting with a lowercase letter and then returns 1 if
only lowercase, 0 if not.
How can I do that?

example list:

myList = [ "abc", "def", "Cde"]

If I write:

def checkIfOnlyLower(list):
     for item in list:
          if item[0].islower():
             return 1
         else:
            return 0

the function will return 1 as soon as it comes to the first string starting
with a lowercase letter.But that's not what I want.

Hope you can help me.

Thanks in advance.


Nicole


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor