1

I am trying to run a loop within a loop and I am totally confused.

for i, value in enumerate(len(sections):
    if i not in [17, 24]:

        if (' tax ' in sections[i]
        or ' Tax ' in sections[i]):

            pat=re.compile("|".join([r"\b{}\b".format(m) for m in months]), re.M)
            month = pat.search("\n".join(sections[i].splitlines()[0:6]))
            print(month)

The problem is that I want to run the loop for all values in len(sections) except 17 and 24. The idea is the following: for each section (article), if the word tax or tax is in it, print the month. Everything is working but the lines at the beginning, where I am trying to run the loop except the values 17 and 24.

Cheers,

6
  • 1
    You can't enumerate a length. You mean for i, value in enumerate(sections): Commented Feb 1, 2016 at 12:22
  • 1
    The code you posted is full of syntax errors. Commented Feb 1, 2016 at 12:22
  • 1
    change enumerate(len(section)) to range(len(section)). You can't enumerate an integer Commented Feb 1, 2016 at 12:24
  • @ Glostas, I just changed it and it works, cheers Commented Feb 1, 2016 at 12:28
  • 2
    Don't edit your question to remove the problem you are trying to solve. Either accept the given answer, or delete the question. Commented Feb 1, 2016 at 12:42

2 Answers 2

4

This should work:

for i, value in enumerate(sections):
    if i not in [17, 24]:
        if ' tax ' in sections[i] or ' Tax ' in sections[i]:
            pat = re.compile("|".join([r"\b{}\b".format(m) for m in months]), re.M)
            month = pat.search("\n".join(sections[i].splitlines()[0:6]))
            print(month)
Sign up to request clarification or add additional context in comments.

1 Comment

Note: section[i] is value
-1

the syntax : for ( i,j) in enumerate(mylist) returns at once tuple of two values . the first is the index of an element and the second one is value corresponding to this element. Think about it to rebuild your program.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.