Python 如何生成一个序列,其中每个元素由至少六个不同的元素与相同的元素分开

Python 如何生成一个序列,其中每个元素由至少六个不同的元素与相同的元素分开,python,python-2.7,Python,Python 2.7,从8个可能的字母列表中,我想生成一个随机序列,其中每个元素与一个相同的元素之间至少有六个不同的元素 sequence_list = [] target_list = ["a","b","c","d","e","f","g","h"] for i in range(1,41): sequence_list.append(random.choice(target_list)) print sequence_list 例如,如果序列列表中的第一个字母是a,则不应在列表中至少接下来的6个项目

从8个可能的字母列表中,我想生成一个随机序列,其中每个元素与一个相同的元素之间至少有六个不同的元素

sequence_list = []
target_list = ["a","b","c","d","e","f","g","h"]

for i in range(1,41):
    sequence_list.append(random.choice(target_list))
print sequence_list
例如,如果
序列列表
中的第一个字母是
a
,则不应在列表中至少接下来的6个项目中重复该字母。其他项目也一样。
感谢您的帮助。

这可能不是最有效的方法,但您可以这样做:

>>> target_list = list(string.ascii_letters[:8])
>>> target_list
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
>>> sequence_list = []
>>> for i in range(1,41):
...     el_list = [x for x in target_list if x not in sequence_list[-6:]]
...     sequence_list.append(random.choice(el_list))
... 
>>> 
>>> sequence_list
['e', 'h', 'g', 'a', 'c', 'f', 'd', 'b', 'e', 'h', 'g', 'c', 'f', 'd', 'a', 'e', 'b', 'g', 'c', 'f', 'h', 'a', 'e', 'b', 'g', 'd', 'f', 'c', 'h', 'a', 'b', 'g', 'd', 'f', 'e', 'c', 'a', 'b', 'h', 'd']

这可能不是最有效的方法,但您可以这样做:

>>> target_list = list(string.ascii_letters[:8])
>>> target_list
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
>>> sequence_list = []
>>> for i in range(1,41):
...     el_list = [x for x in target_list if x not in sequence_list[-6:]]
...     sequence_list.append(random.choice(el_list))
... 
>>> 
>>> sequence_list
['e', 'h', 'g', 'a', 'c', 'f', 'd', 'b', 'e', 'h', 'g', 'c', 'f', 'd', 'a', 'e', 'b', 'g', 'c', 'f', 'h', 'a', 'e', 'b', 'g', 'd', 'f', 'c', 'h', 'a', 'b', 'g', 'd', 'f', 'e', 'c', 'a', 'b', 'h', 'd']

你得到了什么输出?我现在只得到了两个项目的列表,比如
['b','c']
。也不确定你的代码到底做了什么你得到了什么输出?我现在只得到了两个项目的列表,比如
['b','c']
。也不确定你的代码到底做了什么