我试图计算以几个后缀结尾的单词的出现次数。我认为那endswith
会接受一个可迭代的;不幸的是,它没有。下面是代码片段:
s = 'like go goes likes liked liked liking likes like'
lst = s.split()
suffixes = ['s', 'es', 'ies', 'ed', 'ing']
counter = 0
prompt = 'like'
for x in lst:
if x.startswith(prompt) and x.endswith(any(suffix for suffix in suffixes)):
counter += 1
的值counter
应该4
在执行结束时。这是显示的错误消息:
TypeError: endswith first arg must be str or a tuple of str, not bool
我怎样才能让上面的代码工作?