python - python随机选择没有重复
问题描述
大家好,我想避免在我的聊天机器人和聊天机器人中出现重复的问题,只需询问列表中的三个问题,然后返回感谢您的帮助
@app.route("/get")
def get_bot_response():
symptom = request.args.get('msg')
questions = ['What is altin cuzdan?', 'how we can sell the gold?', 'how we can buy the gold?','who are you?', 'how we can change our password?']
#time.sleep(5)
#sampling = random.choice(questions, )
questions2 = questions[:]
questions2.remove(quesuions)
return random.choice(questions2)
解决方案
以这种方式用“提问”列表作为论据吗?正如@usr2564301 所评论的那样,无论哪种方式,您都必须存储在功能之外提出的问题。
@app.route("/get")
def get_bot_response(norepeat):
symptom = request.args.get('msg')
questions = ['What is altin cuzdan?', 'how we can sell the gold?', 'how we can buy the gold?','who are you?', 'how we can change our password?']
#time.sleep(5)
#sampling = random.choice(questions, )
questions2 = questions[:]
questions2.remove(questions)
while True:
return_question = random.choice(questions2)
if return_question in norepeat:
pass
else:
break
return random.choice(questions2)
norepeat = []
while True:
question = get_bot_response(norepeat)
norepeat.append(question)
推荐阅读
- javascript - 我试图提醒“70”和“John”,但我收到了一个提醒“[object Object]”
- javascript - 如何始终保持 Plotlly.js 中的峰值或以编程方式触发它们?
- .htaccess - 如何将字符串添加到 htaccess 中的 url
- c# - 从 64 位应用程序调用 32 位 DLL 的函数
- python - python并附加到JSON文件
- delphi - 级联删除EntityDAC中的一对一链接实体
- php - 插件在激活过程中产生了2651个字符的意外输出,为什么?
- c++ - 替换字符串 C++ 位置 X 中的单词
- pandas - 在 Pandas 多索引 DataFrame 上扩展指数权重,其中每一天都是一个矩阵
- javascript - Javascript表单未提交