python 避免从列表中随机选择先前值

2j4z5cfb  于 12个月前  发布在  Python
关注(0)|答案(5)|浏览(142)

我希望我的代码能打印出5个wrong()函数,而不是一行中有2个相同的文本。
我想确保如果我使用这个函数至少两次,我100%确定前一个值不会与下一个相同。
例如,我想避免的是:

Wrong!
Wrong!

字符串
虽然这仍然很好:

Wrong!
Incorrect!
Wrong!


我的代码:

import random

def wrong():
    wrong_stats=["\n Wrong!","\n Tough Luck!","\n Better Luck Next Time!","\n Not there yet!","\n Incorrect!"]
    rand = random.choice(wrong_stats)
    rand3 = random.choice(wrong_stats)
    norep(rand,rand3,wrong_stats)

def norep(rand,rand3,wrong_stats):
    if rand == rand3:
        same = True
        while same:
            rand = random.choice(wrong_stats)
            if rand != rand3:
                print(rand)
                break
            
    elif rand != rand3:
        print(rand)

wrong()
wrong()
wrong()
wrong()
wrong()

7lrncoxx

7lrncoxx1#

您需要跟踪它最后返回的值;您可以

  • 使用一个全局模块来实现 (实际上通常很混乱)
  • 或者把它变成一个类 (有点冗长)
  • 或者在外部跟踪并每次传递它 (笨重而乏味)

但imo最好的方法是将wrong函数转换为generator:这样你就可以在生成器执行状态中跟踪最后一个返回值,并在下次避免它,而不必担心外部代码中的任何地方。

def wrong():
    wrong_stats = ["Wrong!","Tough Luck!","Better Luck Next Time!","Not there yet!","Incorrect!"]
    previous_value = None
    while True:
        value = random.choice(wrong_stats)
        if value != previous_value:
            yield value
            previous_value = value

字符串
和用法:

w = wrong()
for i in range(5):
    print(next(w))

# Tough Luck!
# Incorrect!
# Not there yet!
# Tough Luck!
# Better Luck Next Time!


你可以继续使用生成器调用next,它将产生无限数量的字符串,而不会重复以前的值。

zc0qhyus

zc0qhyus2#

全局变量是一个糟糕的做法。
你应该把你最后打印的值传递给wrong,然后在除了那个值之外的所有值中进行选择。像这样:

import random

def wrong(last):
    chosen = random.choice([stat for stat in WRONG_STATS if stat != last])
    print(chosen)
    return chosen

if "__main__" == __name__:
    last = None
    for i in xrange(5):
        last = wrong(last)

字符串

vbopmzt1

vbopmzt13#

将前一个值存储在全局变量中,并从列表中随机选择(不包括前一个值):

import random

wrong_stats=["\n Wrong!","\n Tough Luck!","\n Better Luck Next Time!","\n Not there yet!","\n Incorrect!"]
prev = ""

def wrong():
  global prev
  if prev == "":
    prev = random.choice(wrong_stats)
  else:
    prev = random.choice(wrong_stats[:wrong_stats.index(prev)] + wrong_stats[wrong_stats.index(prev)+1:])
  print prev
if __name__ == "__main__":
  wrong()
  wrong()
  wrong()
  wrong()
  wrong()

字符串

5ktev3wc

5ktev3wc4#

使用random.shuffle:

from random import shuffle
>>> shuffle(wrong_stats)
>>> wrong_stats
['\n Better Luck Next Time!', '\n Wrong!', '\n Incorrect!', '\n Tough Luck!', '\n Not there yet!']
>>> shuffle(wrong_stats)
>>> wrong_stats
['\n Incorrect!', '\n Tough Luck!', '\n Better Luck Next Time!', '\n Not there yet!', '\n Wrong!']
>>> shuffle(wrong_stats)
>>> wrong_stats
['\n Incorrect!', '\n Wrong!', '\n Better Luck Next Time!', '\n Not there yet!', '\n Tough Luck!']
>>> shuffle(wrong_stats)
>>> wrong_stats
['\n Wrong!', '\n Incorrect!', '\n Better Luck Next Time!', '\n Tough Luck!', '\n Not there yet!']

字符串

tzxcd3kk

tzxcd3kk5#

这里有另一个例子,可能会给你一个给予的想法:

import random 

country = ["Spain", "Sweden", "Netherlands", "Germany"]
lastcountry = ''
i = 0
while i <= 5:
    country = (random.choice(country))
    if (country != lastcountry):
        i = i + 1
        print (country)
    lastcountry = country

字符串
它会记住最后选择的选项。
如果它做了一个新的选择,它会看它是否和以前的选择不一样。

相关问题