[Tutor] beginsWith multiple prefixes

Emad Nawfal (عماد نوفل) emadnawfal at gmail.com
Thu Dec 25 12:10:51 CET 2008


On 12/25/08, Bill Campbell <bill at celestial.net> wrote:
>
> On Wed, Dec 24, 2008, bob gailer wrote:
> > Emad Nawfal (???? ????) wrote:
> >> Hi Tutors,
> >> I want a function that acts like the startswith method, but can take
> >> multiple prefixes. As an amateur programmer, I came up with this one,
> >> and it works fine, but my experience tells me that my solutions are
> >> not always the best ones around. Can you please tell me what a better
> >> option might be:
>
> ...
>
> > Above does a lot more work than necessary. Try:
> >
> > def beginsWith(word, listname):
> >    for prefix in listname:
> >       if word.startswith(prefix):
> >          return True
>
>
> It might be more useful to return the prefix that matched as the
> caller already knows what ``word'' is.
>
> Bill
> --
> INTERNET:   bill at celestial.com  Bill Campbell; Celestial Software LLC
> URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
> Voice:          (206) 236-1676  Mercer Island, WA 98040-0820
> Fax:            (206) 232-9186
>
> Basic Definitions of Science:
>     If it's green or wiggles, it's biology.
>     If it stinks, it's chemistry.
>     If it doesn't work, it's physics.
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>

Thank you so much Bob and Bill. This is really much better than mine. Bill's
suggestion is not applicable in my current script, but I believe I will need
that soon.
Thank you both.
Thank you all tutors


-- 
لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.....محمد
الغزالي
"No victim has ever been more repressed and alienated than the truth"

Emad Soliman Nawfal
Indiana University, Bloomington
http://emnawfal.googlepages.com
--------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20081225/629bf74a/attachment.htm>


More information about the Tutor mailing list