Python |避免从列表中随机选择的先前值

时间:2015-04-22 17:36:32

标签: python random

基本上我想让我的代码做的就是打印出5 wrong()个func,而不会有相同文本的2行。当然,我不希望它是幸运的。 :)

虽然不用担心它的部分打印5 wrong() s,但我只是想确定我是否至少使用此功能两次我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()

5 个答案:

答案 0 :(得分:1)

全局变量是一种不好的做法。

您应该将最后打印的值传递给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)

答案 1 :(得分:1)

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

  • 为此使用全局模块(通常在实践中很乱)
  • 或将其转换为类(详细类)
  • 或保持外部跟踪并在每次(笨重而乏味)时传递,

但最好的方法是将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,它将生成无限数量的字符串,而不会重复前一个值。

答案 2 :(得分:0)

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

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()

答案 3 :(得分:0)

使用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!']

答案 4 :(得分:0)

这是另一个可能给你一个想法的例子:

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

它会记住最后一个被选中的选项。
如果它进行了新的选择,它看起来是否与普通选择不同。