简体   繁体   English

如何从python中的排名组中随机选择数字,以创建特定长度的列表

[英]How to randomly pick numbers from ranked groups in python, to create a list of specific length

I am trying to create a sequence of length 6 which consists of numbers randomly picked from ranked groups. 我正在尝试创建一个长度为6的序列,该序列由从排名组中随机选择的数字组成。 The first element of the sequence has to be drawn from the first group, and the last element has to be drawn from the last group . 序列的第一个元素必须从第一个组中提取,而最后一个元素必须从最后一个组中提取

Let the new sequence be called "seq". 将新序列称为“ seq”。 Then, if 那如果

a = [1,2,3]
b = [9]
c = [5,6]
d = [11,12,4]

seq[0] in a == 1
seq[-1] in d == 1

The intermediate elements have to come from lists a,b,c,d. 中间元素必须来自列表a,b,c,d。 But, if the second element is randomly drawn from 'a', then the third one, has to be drawn either from a later 'a' element, or from b/c/d. 但是,如果第二个元素是从'a'中随机抽取的,则第三个元素必须从后一个'a'元素或b / c / d中抽取。 Similarly, if the third element is drawn from 'c', then the other ones have to come from later ranks like d.The groups are ranked this way. 同样,如果第三个元素是从'c'中提取的,则其他元素必须来自后来的排名,例如d。

The number of groups given now, is arbitrary (maximum of 6 groups). 现在给出的组数是任意的(最多6组)。 The length for the sequence ( len(seq) == 6 ) is standard. 序列的长度(len(seq)== 6)是标准长度。

One element from each group has to be in the final sequence. 每一组中的一个元素必须位于最后的顺序中。 Repetition of elements is not allowed. 不允许重复元素。 All group elements are unique (and they are always numbers in the range of 1-12). 所有组元素都是唯一的(它们始终是1到12之间的数字)。

How about this: 这个怎么样:

from random import choice, randint

v = [[1, 2, 3],
      [9],
      [5, 6],
      [11, 12, 4]]


def whatever(values, n=6):
    first = [choice(values[0])]
    last = [choice(values[-1])]
    seq = []
    k = 0
    while len(seq) < n -2:
        k = randint(k, len(values)-1)
        seq.append(choice(values[k]))
    return first + seq + last

print whatever(v, 6)

you have four forced choices, then two free choices. 您有四个强制选择,然后有两个自由选择。 set is a good help here. set是一个很好的帮助。

from random import choice
a = [1,2,3]
b = [9]
c = [5,6]
d = [11,12,4]

l=a+b+c+d #ordered candidates

def select():
    e=set(l)
    for s in (a,b,c,d,e,e):              # 4 forced choices and 2 frees.
        e.remove(choice(tuple(s)))       # sets have no index.
    return [x for x in l if x not in e]

10 samples : 10个样品

>>> for _ in range(10) : print (select())
[1, 9, 5, 11, 12, 4]
[1, 3, 9, 6, 11, 4]
[1, 3, 9, 5, 6, 12]
[1, 2, 9, 6, 11, 4]
[1, 2, 9, 5, 6, 4]
[2, 9, 5, 6, 11, 4]
[1, 2, 9, 5, 11, 12]
[1, 3, 9, 6, 11, 12]
[3, 9, 6, 11, 12, 4]
[1, 2, 9, 5, 12, 4]

Try z=[a,b,c,d] 尝试z = [a,b,c,d]

Then for each element e in z, do seq.append(e[randint(0,Len(e))]), eg 然后对z中的每个元素e进行seq.append(e [randint(0,Len(e))]),例如

from random import randint

a=[1,2,3]
b=[4,5,6]
z=[a,b]
print(a[randint(0,len(a))])
f=[]
for e in z:
    f.append(e[randint(0,len(e))])
print(f)

Or you could use a for loop instead of a for each and use a manual counter so you could have seq[counter]=... 或者,您可以使用for循环而不是每个for循环,并使用手动计数器,这样就可以使seq [counter] = ...

This will not pick the groups randomly though. 但是,这不会随机选择组。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM