Python - 列表检查某些内容是否已经随机化

标签 python list

好的,所以我正在用 python 创建一个程序,基本上它是一个针对手机用户的故障排除程序,到目前为止它非常基本,我现在的情况是我正在创建一个要问的问题列表。

我将使用随机模块从故障排除问题列表中随机化一个字符串,但我不希望它随机化列表中的第一个问题,然后再次随机化列表中的第一个问题。

所以真正的问题是;如何检查随机字符串是否已被随机化,如果已随机化,我希望我的程序从列表中随机化另一个字符串,如果已经说过,则随机化另一个字符串,如果不使用该字符串,等等。

注意:这个程序还没有完成,我现在才刚刚开始,所以我在最后调用这个函数,这样我就可以在不同的时间运行该程序来看看它是否有效。

import random

Questions = [
            'Has your phone gotten wet?', 'Have you damaged your screen?', 'Is the phone at full battery?',
            'Has your phone been dropped?', ' Does the mobile phone turn itself off?', 'Does the device keep crashing',
            'Does the phone keep freezing?', 'Can you not access the internet on your phone?', 'Is the battery draining quickly?',
            'Can you not access certain files on your phone?'
            ]
Solutions = [
            'Put your mobile phone inside of a fridge, it sounds stupid but it should work!', 'Try turning your device on and off',
            'Visit your local mobile phone store and seek help'
        ]
def PhoneTroubleshooting():
    print('Hello, welcome to the troubleshooting help section for your mobile phone.\n'
            'This troubleshooting program is going to ask you a series of questions, to determine the issue with your device.')
    answer = print(random.choice(Questions))
    if answer == 'yes':
            print('Okay, I have a solution', random.choice(Solutions))

    else: print('Okay, next problem')

PhoneTroubleshooting()

最佳答案

您应该使用 shuffle 随机化整个列表,而不是一次选择一个随机元素。 ,然后迭代它。即

random.shuffle(Questions)   # This shuffles `Questions` in-place

print(Questions[0])
...

但是请注意,您可能希望保持两个列表的协调性 - 即您仍然希望答案与您的问题相匹配,因此您应该随机化索引而不是值:

inds = range(len(Questions))
random.shuffle(inds)
print(Questions[inds[0]])
...
print(Answers[inds[0]])

关于Python - 列表检查某些内容是否已经随机化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33810088/

相关文章:

python - 将 numpy 多维数组转换为列表

python - 如何使用 Python 通过 IMAP 访问 GMail 上的所有电子邮件?

python - 创建特定模式的两个列表的快速方法

r - 使用三点参数时的 list(...) 与 as.list(...)

python - 如何使用 Python 从电子邮件内容中获取附加的 eml 文件?

python - 'str' 对象没有属性 'map'

python - 使用按查询排序的无序结果

python - 使用生成器或理解从字典中创建一个二元组列表?

java - 用空格分隔列表项,用选项卡分隔更高级别的指标

c# - 将列表创建的字典从 Python 转换为 C#