-2

I have a simple set that contains strings and some of those strings are numbers. (i.e. '1', '45', '5', '39').

Some of the strings have dashes (-) or plus (+) signs in them. (i.e. '55+', '1-3').

I have created a function that should parse the set and remove the digits along with removing the characters and remove any words that are capitalized.

Function:

def parse_set(s):
    for word in s.copy():
        word.strip('+-')
        if word.istitle() or word.isdigit():
            s.remove(word)

    completed_set = set(filter(None, s))

    return completed_set

But when I pass in a set, any regular number without any - or + are removed. But numbers that do contain the characters still remain.

I had thought that the .strip() method would take care of that?

kstullich
  • 651
  • 1
  • 11
  • 27

1 Answers1

1

As Rawing noted in his comment, strip doesn't work in place, and strings are immutable, so you have to assign back the result to word again:

word = word.strip('+-') # Rawing 
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
stovfl
  • 14,998
  • 7
  • 24
  • 51