【问题标题】:how to Return a string that is pressent in two different lists?如何返回两个不同列表中存在的字符串?
【发布时间】:2021-04-08 21:00:13
【问题描述】:

您好,我是 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)

【问题讨论】:

    标签: python string list split python-webbrowser


    【解决方案1】:

    我认为在这种情况下,您可以将语音列表指定为一个集合,并将您的 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)
    

    【讨论】:

    • 谢谢,但是如何创建一个包含 Word_A 和 Word_B 之间的字符串/集?
    • 可以套用样例idea:final_set = Word_A.intersection(Word_B)
    猜你喜欢
    • 2022-11-17
    • 2021-07-31
    • 2021-03-11
    • 2015-10-13
    • 1970-01-01
    • 2020-12-11
    • 2010-11-05
    • 1970-01-01
    • 2015-05-07
    相关资源
    最近更新 更多