问题
I would like to know how i could have a variable A
to be any random string from ListA
and B
to be any random string from ListB
?
I would like to use this in a voice assistant and Voice
would be the string containing the recognized sentence.
It should make a google search of anything I say between string A
and string B
.
import re
import webbrowser
ListA = ["search", "research"]
ListB = ["on google", "using google"]
A = # any string from ListA
B = # any string from ListB
Search = re.search(A + '(.+?)' B + , Voice).group(1)
url = "https://www.google.com/search?q=" + Search
webbrowser.get().open(url)
回答1:
I suggest reading what exists in the random module.
This is one of the things it offers:
list_a = ["search", "research"]
a = random.choice(list_a)
来源:https://stackoverflow.com/questions/65535646/how-to-use-any-string-from-list-as-a-variable