Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个后缀列表,我想检查我的单词是否以其中任何一个结尾,如果是,我想打印它,我正在执行以下操作:
if(string.endswith(any(word in my_list))): print string print" " print word
my_list 是后缀列表。当我运行它时,它给了我一个错误,说没有定义名称'word'
any返回一个布尔值。 str.endswith需要一个字符串或一个tuple字符串。
any
str.endswith
tuple
你可能想要这样的东西:
if s.endswith(tuple(my_list)): print string
或者如果你真的想知道它匹配的是哪一个:
suffix = next((word for word in my_list if s.endswith(word)),False) if suffix: print word, suffix