简体   繁体   English

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

[英]How to generate a sequence where each element is separated from an identical element by at least six different elements

From a list of 8 possible letters I want to generate a random sequence where each element is separated from an identical element by at least six different elements. 从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

For example if the first letter in sequence_list is an a it should not be repeated for at least the next 6 items in the list. 例如,如果sequence_list的第一个字母是a ,则不应该至少重复列表中的后6个项目。 Same for every other item. 每个其他项目都相同。 Appreciate your help. 感谢您的帮助。

this is probably not the most efficient way of doing it, but you can do it like this: 这可能不是最有效的方法,但你可以这样做:

>>> 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']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从给定的元素列表生成随机的numpy数组,每个元素至少重复一次 - Generate random numpy array from a given list of elements with at least one repetition of each element 如何生成一个假设策略来生成一个列表,该列表至少包含它从中采样的每个元素中的一个? - How can I generate a hypothesis strategy to generate a list that contains at least one of each element it samples from? 如何从n组中每个元素相同的列表中删除n组? - How to Remove n-Tuples From List Where Each Element within the n-Tuples is Identical? Python 如何从组中获取元素的所有组合,每个组中最多有一个元素,并且其中一个组中至少有一个元素 - Python how to get all combinations of elements from groups with at most one element from each group and at least one element from one of the groups 如何随机获取 numpy 数组的一定数量的元素,每个 class 至少有一个元素? - How do I randomly get a certain number of elements of a numpy array with at least one element from each class? 使用在不同条件下具有不同值的元素制作条形图,其中每个元素的值按元素分组和着色 - Making a barplot with elements with different values under different conditions, where values for each element are grouped and colored by element 获取列表中所有元素平均值的最有效方法,其中每个元素的出现次数至少是列表模式的一半 - Most efficient way to get average of all elements in list where each element occurs at least half as many times as the mode of the list 在Python列表中打印最匹配的列表,其中每个元素都在内部分开 - Print the best match in the Python list, where each element is separated internally 创建一个列表列表,其中每个子列表包含来自输入列表序列的一个元素 - Creating a list of lists where each sublist contains one element from an input sequence of lists 在Python中,如何生成数组的排列,其中每个列和行只有一个元素? - In Python, how do you generate permutations of an array where you only have one element from each column and row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM