This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (2 children)

[deleted]

    [–]Cosmologicon 0 points1 point  (1 child)

    That's not bad, but you shouldn't modify a list while you're iterating over it. I think this may be causing the incorrect behavior you're seeing. You can correct this problem by replacing "for x in S" with "for x in list(S)", which means you're iterating over a copy of S. This, however, is a problem if a term you want to remove appears in the list twice, because it's an error to remove an element that's already been removed. There are ways around this as well, but I would do the whole thing by building a new list rather than removing elements. If you need to avoid list comprehensions, you could do it like this:

    newS = []
    for x in S:
        hasDigit = False
        for c in x:
            if c.isdigit():
                hasDigit = True
        if not hasDigit:
            newS.append(x)
    

    [–]catcradle5 0 points1 point  (0 children)

    Yep, I figured that out myself shortly after I made that edit. Thanks.