Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以在python中为random.randint()函数设置条件吗 WORDS=(“lorem”、“ipsum”、“python”) 单词=随机。选择(单词) 正确的 长度=0 当长度_Python_List_Random - Fatal编程技术网

我可以在python中为random.randint()函数设置条件吗 WORDS=(“lorem”、“ipsum”、“python”) 单词=随机。选择(单词) 正确的 长度=0 当长度

我可以在python中为random.randint()函数设置条件吗 WORDS=(“lorem”、“ipsum”、“python”) 单词=随机。选择(单词) 正确的 长度=0 当长度,python,list,random,Python,List,Random,时,您可以使用进行就地洗牌的。例如: WORDS=("lorem","ipsum","python") words = random.choice(WORDS) correct = words length = 0 while length <len(words): i = random.randint(0,len(words)) print(words[i],end=" ") length +=1 要对元组中的每个单词进行无序排列,需要执行以下操作: >&

时,您可以使用进行就地洗牌的。例如:

WORDS=("lorem","ipsum","python")
words = random.choice(WORDS)
correct = words
length = 0
while length <len(words):
    i = random.randint(0,len(words))
    print(words[i],end=" ")
    length +=1
要对元组中的每个单词进行无序排列,需要执行以下操作:

>>> import random
>>> WORDS = ["lorem","ipsum","python"]  # But you need `list` not `tuple`; 
                                        # tuples are immutable
>>> random.shuffle(WORDS)
>>> WORDS  # List is shuffled
['ipsum', 'python', 'lorem']
shuffled_words
列表中的值为:

shuffled_words= []

for word in WORDS:
    word_list = list(word)  # Typecast `str` to `list`
    random.shuffle(word_list)  # shuffle the list
    shuffled_words.append(''.join(word_list)) # join the list to make `str`

不,但您可以循环,直到您得到一个尚未看到的号码:

>>> shuffled_words
['omelr', 'spmiu', 'pynhot']

使用
random.sample
(使用
random.shuffle
需要创建一个列表,因为它可以正常工作):

顺便说一句:你的代码肯定会崩溃,因为它使用的是
random.randint
,它可能会选择
len(words)
,超出范围。
您应该选择了
random.randrange

我已经为您编写了一个相当简单的类,稍后将详细介绍其用法

shuffled = "".join(random.sample(words,len(words)))
我创建了这个
FairDice
类。您可以通过调用
.roll()
函数进行常规滚动。或者,您可以通过调用
statefull\u roll()
方法来执行滚动,以记住滚动的内容。如果没有剩余的数字,则不会返回任何数字。您还可以调用
忘记历史记录()
函数来忘记历史记录

下面是一个用法示例:

from random import random
from math import floor

class FairDice():
    def __init__(self, sides = 6):
        self.sides = sides
        self.history = []
        self.last = None
    def roll(self):
        number = random()
        roll = int(floor(number * self.sides)) + 1
        return roll

    def statefull_roll(self, remember = None):
        while True:
            roll = self.roll()
            remember = self.sides if remember is None else remember
            if len(self.history) >= remember:
                return None

            if roll not in self.history:
                self.history.append(roll)
                return roll

    def forget_history(self):
        self.history = []  

    def forget_one(self):
        self.history = self.history[1:]
假设在5卷后重复字母是可以的。我添加了

dice = FiarDice(25) #creates a 25 sided dice  

for x in range(10):
    print("Regular Roll gives you: ", dice.roll())  

for x in range(25):
    print("Your sort of Roll gives you: ", dice.statefull_roll()) 

print ( dice.statefull_roll()) # Should return none after exhausting all possible characters. 

dice.forget_history() # forgets that any value have been recorded at all  
这将返回无后,5卷之后,所有你要做的是

roll = dice.statefull_roll(remember = 5)  
forget\u one
忘记你最老的一卷


我写的东西不多,但很有效,并且足够灵活,可以在游戏中使用

可能有助于复制,但我想在一个单词中混淆字母,而不是列表/元组中的项目,但我只是一个初学者。你能给我解释一下set()是如何工作的吗?这不是很有效:最后,你会抛出很多随机数来避免已经看到的。你能告诉我为什么“我看到了”在while循环中是真的吗loop@EricWatson如果
i
是所见
集合的成员,表达式
i-in-seen
是正确的。但我如何成为集合的成员发送?我认为,创建列表的副本并在其上执行洗牌操作比在
random.sample
返回的列表上执行
join
并计算
len(words)
要好。可能是基于观点更确切地说,
sample
的用例是当您需要的项目少于您从中抽取样本的人群时<代码>示例
比洗牌慢,因为它做得更多,例如,它分配一个单独的集合来跟踪它已经看到的项目。因此,
y=random.sample(x,len(x))
是表达
y=x[:]的低效方法;随机。随机移动(y)
dice = FiarDice(25) #creates a 25 sided dice  

for x in range(10):
    print("Regular Roll gives you: ", dice.roll())  

for x in range(25):
    print("Your sort of Roll gives you: ", dice.statefull_roll()) 

print ( dice.statefull_roll()) # Should return none after exhausting all possible characters. 

dice.forget_history() # forgets that any value have been recorded at all  
roll = dice.statefull_roll(remember = 5)  
if roll is None:
    dice.forget_one()  
    roll = dice.statefull_roll(remember = 5)