gpt4 book ai didi

python - python中的前缀匹配

转载 作者:太空狗 更新时间:2023-10-29 22:16:36 29 4
gpt4 key购买 nike

我有一个像这样的字符串:

" This is such an nice artwork"

我有一个 tag_list ["art","paint"]

基本上,我想写一个接受这个字符串和标签列表作为输入的函数并返回“艺术品”一词,因为艺术品包含标签列表中的艺术一词。

我如何最有效地做到这一点?

我希望它在速度方面是高效的

 def prefix_match(string, taglist):
# do something here
return word_in string

最佳答案

尝试以下操作:

def prefix_match(sentence, taglist):
taglist = tuple(taglist)
for word in sentence.split():
if word.startswith(taglist):
return word

这是有效的,因为 str.startswith()可以接受前缀元组作为参数。

请注意,我将 string 重命名为 sentence,因此字符串模块没有任何歧义。

关于python - python中的前缀匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10728524/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com