How to use any string from list as a variable?

半腔热情 提交于 2021-01-07 02:54:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!