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

2j4z5cfb  于 2024-01-05  发布在  Python
关注(0)|答案(5)|浏览(173)

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

  1. Wrong!
  2. Wrong!

字符串
虽然这仍然很好:

  1. Wrong!
  2. Incorrect!
  3. Wrong!


我的代码:

  1. import random
  2. def wrong():
  3. wrong_stats=["\n Wrong!","\n Tough Luck!","\n Better Luck Next Time!","\n Not there yet!","\n Incorrect!"]
  4. rand = random.choice(wrong_stats)
  5. rand3 = random.choice(wrong_stats)
  6. norep(rand,rand3,wrong_stats)
  7. def norep(rand,rand3,wrong_stats):
  8. if rand == rand3:
  9. same = True
  10. while same:
  11. rand = random.choice(wrong_stats)
  12. if rand != rand3:
  13. print(rand)
  14. break
  15. elif rand != rand3:
  16. print(rand)
  17. wrong()
  18. wrong()
  19. wrong()
  20. wrong()
  21. wrong()

7lrncoxx

7lrncoxx1#

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

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

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

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

字符串
和用法:

  1. w = wrong()
  2. for i in range(5):
  3. print(next(w))
  4. # Tough Luck!
  5. # Incorrect!
  6. # Not there yet!
  7. # Tough Luck!
  8. # Better Luck Next Time!


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

展开查看全部
zc0qhyus

zc0qhyus2#

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

  1. import random
  2. def wrong(last):
  3. chosen = random.choice([stat for stat in WRONG_STATS if stat != last])
  4. print(chosen)
  5. return chosen
  6. if "__main__" == __name__:
  7. last = None
  8. for i in xrange(5):
  9. last = wrong(last)

字符串

vbopmzt1

vbopmzt13#

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

  1. import random
  2. wrong_stats=["\n Wrong!","\n Tough Luck!","\n Better Luck Next Time!","\n Not there yet!","\n Incorrect!"]
  3. prev = ""
  4. def wrong():
  5. global prev
  6. if prev == "":
  7. prev = random.choice(wrong_stats)
  8. else:
  9. prev = random.choice(wrong_stats[:wrong_stats.index(prev)] + wrong_stats[wrong_stats.index(prev)+1:])
  10. print prev
  11. if __name__ == "__main__":
  12. wrong()
  13. wrong()
  14. wrong()
  15. wrong()
  16. wrong()

字符串

展开查看全部
5ktev3wc

5ktev3wc4#

使用random.shuffle:

  1. from random import shuffle
  2. >>> shuffle(wrong_stats)
  3. >>> wrong_stats
  4. ['\n Better Luck Next Time!', '\n Wrong!', '\n Incorrect!', '\n Tough Luck!', '\n Not there yet!']
  5. >>> shuffle(wrong_stats)
  6. >>> wrong_stats
  7. ['\n Incorrect!', '\n Tough Luck!', '\n Better Luck Next Time!', '\n Not there yet!', '\n Wrong!']
  8. >>> shuffle(wrong_stats)
  9. >>> wrong_stats
  10. ['\n Incorrect!', '\n Wrong!', '\n Better Luck Next Time!', '\n Not there yet!', '\n Tough Luck!']
  11. >>> shuffle(wrong_stats)
  12. >>> wrong_stats
  13. ['\n Wrong!', '\n Incorrect!', '\n Better Luck Next Time!', '\n Tough Luck!', '\n Not there yet!']

字符串

tzxcd3kk

tzxcd3kk5#

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

  1. import random
  2. country = ["Spain", "Sweden", "Netherlands", "Germany"]
  3. lastcountry = ''
  4. i = 0
  5. while i <= 5:
  6. country = (random.choice(country))
  7. if (country != lastcountry):
  8. i = i + 1
  9. print (country)
  10. lastcountry = country

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

展开查看全部

相关问题