[Tutor] Best Practice: Subroutines and Loop logic
Benno Lang
transmogribenno at gmail.com
Fri Sep 4 01:23:43 CEST 2009
On Fri, Sep 4, 2009 at 3:29 AM, GoodPotatoes<goodpotatoes at yahoo.com> wrote:
> Hello,
>
> I am trying to find the best way to do this. My goal is to only call this
> subroutine only ONCE, and the logic is contingent upon two outputs.
>
> lWords=[...] # List of many, many words
> lData=[...] #list of some words that need to be validated against lWords
>
> #subroutine to search for words, ignore case
> def sub1(foo):
> pFoo=re.compile(foo,re.I)
> for word in lWords:
> if re.search(pFoo,word):
> return[1,word]
>
> #logic loop
> for word in lData:
> if word in lWords:
> continue
> elif sub1(word)[0]=1:
> word=sub1(word)[1] # <--- Here is my question.
> else:
> print word " not found.\n"
>
> The subroutine is being run once at the elif statement. I don't want to run
> it again just to get the [1] value.
> *Is there any way to capture all of the values returned when it is run
> during the elif statement?
Yes. BTW, since you have a continue, I don't think you need the elif.
> *Is this actually running twice?
Yes, but it needn't.
I think you would do something like this instead:
if word in lWords:
continue
result = sub1(word):
if result[0] == 1:
word = result[1]
else:
print word " not found.\n"
My apologies if this doesn't make any sense. I'm very much a beginner in Python.
HTH,
benno.
More information about the Tutor
mailing list