首页 > 解决方案 > 没有术语重复的随机字符串?

问题描述

我正在尝试在 Python 中随机化一些单词,但我得到带有重复术语的行。我怎样才能避免它?

我的代码:


words = ['blue,', 'orange,', 'yellow,', 'white,','pink,']

list=[]
for i in range(90):
    r = ''.join(random.choices(words, k=random.randint(1, 5)))
    if r not in list: list.append(r) 

我得到什么:


orange,white,orange,white,blue,  <-- Here Orange & white are twice

谢谢!

标签: python-3.x

解决方案


推荐阅读