问题描述
嗨,我是 Python 新手,我正在制作语音助手。
Voice 字符串包含我使用 Speech.recognition 所说的内容。
我想将 Voice 字符串拆分成一个列表并返回匹配的字符串。
例如:Voice =“嗨,你可以在谷歌上研究这个和那个吗?”
我以不同的方式询问了它,但我认为我没有正确询问我在寻找什么。 How to use any string from list as a variable?
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 指定为另一个集合。然后就可以直接应用交集函数了。
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)