Python 随机列表选择:如何确保同一项目不存在';I don’我从来没有连续重复两次,一个接一个?

Python 随机列表选择:如何确保同一项目不存在';I don’我从来没有连续重复两次,一个接一个?,python,python-3.x,list,random,Python,Python 3.x,List,Random,例如,我如何确保Hello不会连续重复两次?如果以后再重复它们就可以了,只是不要在之后立即重复。使用random.sample而不是random.choice 像其他建议的答案一样,使用random.sample,只有在您知道您只需要一定数量的项目时才有用,如2项。确保随机性和无重复的最佳方法是使用random.shuffle: import random welcomes = ["Hello","Hi","What's up","YO", "Piss off"] chosen = rand

例如,我如何确保
Hello
不会连续重复两次?如果以后再重复它们就可以了,只是不要在之后立即重复。

使用
random.sample
而不是
random.choice


像其他建议的答案一样,使用
random.sample
,只有在您知道您只需要一定数量的项目时才有用,如2项。确保随机性和无重复的最佳方法是使用
random.shuffle

import random

welcomes = ["Hello","Hi","What's up","YO", "Piss off"]

chosen = random.sample(welcomes,2)

for item in chosen:
  print("You have entered the welcome cave ----- {} -----".format(item))
这很好地将列表洗牌到位,然后您可以开始从列表中弹出项目,直到完成:

import random
welcomes = ["Hello","Hi","What's up","YO", "Piss off"]
random.shuffle(welcomes)

这将适用于任何长度的列表,您可以使用此过程,直到完成整个列表。如果您想让整个过程永远持续下去,而不仅仅是在列表结束之前,您还可以在整个过程中添加另一个循环。

如果您想生成一个非常长的问候语流,则具有以下属性:没有连续的问候语是相同的(在线演示:):

或者,当您担心确定性时间时,交换元素(使用第一个):

或随机:

if welcomes[0] == last_hello:
    welcomes[0], welcomes[1] = welcomes[1], welcomes[0]

您可以通过点击&未点击的方式完成:

if welcomes[0] == last_hello:
    swap_with = random.randrange(1, len(welcomes))
    welcomes[0], welcomes[swap_with] = welcomes[swap_with], welcomes[0]
可以使用和权重(需要python-3.6)对其进行优化,但这种方法应该适用于所有python版本:

import random

class RandomChoiceNoImmediateRepeat(object):
    def __init__(self, lst):
        self.lst = lst
        self.last = None
    def choice(self):
        if self.last is None:
            self.last = random.choice(self.lst)
            return self.last
        else:
            nxt = random.choice(self.lst)
            # make a new choice as long as it's equal to the last.
            while nxt == self.last:   
                nxt = random.choice(self.lst)
            # Replace the last and return the choice
            self.last = nxt
            return nxt

或者,如果您不喜欢命中和未命中,您也可以在0和列表长度之间绘制一个随机索引-2,如果它等于或高于上一个,则添加1。这确保不会发生重复,只需调用一次
random
即可获得下一个选择:

>>> welcomes = ["Hello","Hi","What's up","YO", "Piss off"]
>>> gen = RandomChoiceNoImmediateRepeat(welcomes)
>>> gen.choice()
'YO'

我的想法:我们创建两个相同的列表。在循环中,我们从一个列表中弹出一个值,如果该列表的长度小于原始列表-1,我们将列表重置为其原始状态:

import random

class RandomChoiceNoImmediateRepeat(object):
    def __init__(self, lst):
        self.lst = lst
        self.lastidx = None

    def choice(self):
        if self.lastidx is None:
            nxtidx = random.randrange(0, len(self.lst))
        else:
            nxtidx = random.randrange(0, len(self.lst)-1)
            if nxtidx >= self.lastidx:
                nxtidx += 1
        self.lastidx = nxtidx
        return self.lst[nxtidx]

示例对,如
random.Sample(欢迎,2)
您可以使用循环来保存代码重复
import random

class RandomChoiceNoImmediateRepeat(object):
    def __init__(self, lst):
        self.lst = lst
        self.last = None
    def choice(self):
        if self.last is None:
            self.last = random.choice(self.lst)
            return self.last
        else:
            nxt = random.choice(self.lst)
            # make a new choice as long as it's equal to the last.
            while nxt == self.last:   
                nxt = random.choice(self.lst)
            # Replace the last and return the choice
            self.last = nxt
            return nxt
>>> welcomes = ["Hello","Hi","What's up","YO", "Piss off"]
>>> gen = RandomChoiceNoImmediateRepeat(welcomes)
>>> gen.choice()
'YO'
import random

class RandomChoiceNoImmediateRepeat(object):
    def __init__(self, lst):
        self.lst = lst
        self.lastidx = None

    def choice(self):
        if self.lastidx is None:
            nxtidx = random.randrange(0, len(self.lst))
        else:
            nxtidx = random.randrange(0, len(self.lst)-1)
            if nxtidx >= self.lastidx:
                nxtidx += 1
        self.lastidx = nxtidx
        return self.lst[nxtidx]
import random

origin = ["Hello","Hi","What's up","YO", "Piss off"]
welcomes = origin.copy()

for i in range(5):
    if len(welcomes) < len(origin) - 1:
        welcomes = origin.copy()
    random.shuffle(welcomes) # shuffle
    chosen = welcomes.pop() # pop one value
    print("You have entered the welcome cave ----- {} -----".format(chosen))
You have entered the welcome cave ----- Piss off -----
You have entered the welcome cave ----- YO -----
You have entered the welcome cave ----- Piss off -----
You have entered the welcome cave ----- YO -----
You have entered the welcome cave ----- What's up -----