0

I am trying to find the third person singular form of a verb passed to my function. So far I have this:

suffixes = ['o', 'ch', 's', 'sh', 'x', 'z']

def third_person(s):
    r = ''
    if s.endswith('y'):
        r = s[:-1]
        r += 'ies'
    else:
        r += s + 's'
    return r

Which works, but for the case that the verb ends with any of the suffixes in suffixes list, I want to add 'es' to the verb. I have tried this:

if s.endswith(c for c in suffixes):
    s += 'es'

But python says that I can't pass a generator to a function. I could move the if block inside a for loop but then I would have to change other checks as well. How do I do this without wrapping the if block with a for loop?

Mertcan Ekiz
  • 691
  • 1
  • 9
  • 22
  • 1
    The problem is solved, but the question in the title is not answered (for those arriving here via Google). Well, the syntax to yield an iterator that could be passed to a function is `iter(obj)` (for objects supporting iteration). – Rainald62 Jun 30 '18 at 11:18

2 Answers2

2

str.endswith supports a tuple parameter , in which case it would check if the string ends with any of the strings in the tuple. So you can define suffixes as a tuple and directly use in s.endswith(). Example -

suffixes = ('o', 'ch', 's', 'sh', 'x', 'z')
...
if s.endswith(suffixes):

Demo -

>>> "touch".endswith(('o', 'ch', 's', 'sh', 'x', 'z'))
True

Or if you are not defining the suffixes list, instead you are getting it from somewhere else, you can use tuple() to convert it to a tuple and then directly use in str.endswith .

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
1

str.endswith() method accepts tuple as argument you can convert the list to tuple and pass it to method :

if s.endswith(tuple(suffixes)):
    s += 'es'
Mazdak
  • 105,000
  • 18
  • 159
  • 188