如何使用列表中的任何字符串作为变量? [英] How to use any string from list as a variable?

查看:32
本文介绍了如何使用列表中的任何字符串作为变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何让变量 A 成为来自 ListA 的任何随机字符串和来自 B 的任何随机字符串来自 ListB?

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?

我想在语音助手中使用它,Voice 将是包含已识别句子的字符串.它应该对我在字符串 A 和字符串 B 之间所说的任何内容进行谷歌搜索.

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)

推荐答案

我建议阅读 random 模块.

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)

这篇关于如何使用列表中的任何字符串作为变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆