繁体   English   中英

如何返回两个不同列表中存在的字符串?

[英]how to Return a string that is pressent in two different lists?

嗨,我是 python 的新手,我正在做一个语音助手。

Voice 字符串包含我使用 Speech.recognition 所说的内容。

我正在考虑将语音字符串拆分为一个列表并返回匹配的字符串。

例如:Voice =“嗨,你能在谷歌上研究这个和那个吗”

我以不同的方式问它,但我认为我没有正确地问我在寻找什么。 如何使用列表中的任何字符串作为变量?

Voice_List = Voice.split()

List_A = ["research", "search"]
List_B = ["on google", "using google", "with google"]

Word_A = any_word in Voice_List AND in List_A
Word_B = any_word in Voice_List AND in List_B

webbrowser.get().set(anything between Word_A and Word_B)

我认为在这种情况下,您可以将您的语音列表指定为一个集合,并将您的 list_a 或 list_b 指定为另一个集合。 然后你可以直接申请交集 function。

Voice_Set = set(Voice.split())

Set_A = set(["research", "search"])
Set_B = set(["on google", "using google", "with google"])

Word_A = Voice_Set.intersection(Set_A)
Word_B = Voice_Set.intersection(Set_B)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM