从多个列表中随机创建“n”组合

时间:2017-01-12 22:07:22

标签: python combinations itertools

def models():
    default = [0.6,0.67,2.4e-2,1e-2,2e-5,1.2e-3,2e-5]
    lower = [np.log10(i/10) for i in default]
    upper = [np.log10(i*10) for i in default]
    n = 5
    a = np.logspace(lower[0],upper[0],n)
    b = np.logspace(lower[1],upper[1],n)
    c = np.logspace(lower[2],upper[2],n)
    d = np.logspace(lower[3],upper[3],n)
    e = np.logspace(lower[4],upper[4],n)
    f = np.logspace(lower[5],upper[5],n)
    g = np.logspace(lower[6],upper[6],n)

    combs = itertools.product(a,b,c,d,e,f,g)

    list1 = []

    for x in combs:
        x = list(x)
        list1.append(x)


    return list1

上面的代码返回5 ^ 7 = 78,125个列表的列表。有没有办法可以组合a,b,c,d,e,f,g中的项目,可能是随机的,以创建一个名单10000列表?

1 个答案:

答案 0 :(得分:1)

您可以随机抽取每个阵列并将它们组合起来,特别是如果您不需要保证特定组合不会多次出现:

public class Types {
    List<TypeA> typeAs;
    List<TypeB> typeBs;
}

这是一个避免重复的版本,直到您请求的数据量超过可以提供的数据而不重复:

import numpy as np
import random


def random_models(num_values):
    n = 5
    default = [0.6, 0.67, 2.4e-2, 1e-2, 2e-5, 1.2e-3, 2e-5]
    ranges = zip((np.log10(i/10) for i in default),
                 (np.log10(i*10) for i in default))

    data_arrays = []
    for lower, upper in ranges:
        data_arrays.append(np.logspace(lower, upper, n))

    results = []

    for i in xrange(num_values):
        results.append([random.choice(arr) for arr in data_arrays])

    return results


l = random_models(10000)
print len(l)

另请注意,如果您使用def random_models_avoid_repeats(num_values): n = 5 default = [0.6, 0.67, 2.4e-2, 1e-2, 2e-5, 1.2e-3, 2e-5] # Build the range data (tuples of (lower, upper) range) ranges = zip((np.log10(i/10) for i in default), (np.log10(i*10) for i in default)) # Create the data arrays to sample from data_arrays = [] for lower, upper in ranges: data_arrays.append(np.logspace(lower, upper, n)) sequence_data = [] for entry in itertools.product(*data_arrays): sequence_data.append(entry) results = [] # Holds the current choices to choose from. The data will come from # sequence_data above, but randomly shuffled. Values are popped off the # end to keep things efficient. It's possible to ask for more data than # the samples can give without repeats. In that case, we'll reload # temp_data, randomly shuffle again, and start the process over until we've # delivered the number of desired results. temp_data = [] # Build the lists for i in xrange(num_values): if len(temp_data) == 0: temp_data = sequence_data[:] random.shuffle(temp_data) results.append(temp_data.pop()) return results 将其设为生成器,则可以避免构建结果列表。但是,您也希望使用yield语句来使用结果:

for

您必须像这样使用它:

def random_models_avoid_repeats_generator(num_values):
    n = 5
    default = [0.6, 0.67, 2.4e-2, 1e-2, 2e-5, 1.2e-3, 2e-5]

    # Build the range data (tuples of (lower, upper) range)
    ranges = zip((np.log10(i/10) for i in default),
                 (np.log10(i*10) for i in default))

    # Create the data arrays to sample from
    data_arrays = []
    for lower, upper in ranges:
        data_arrays.append(np.logspace(lower, upper, n))

    sequence_data = []
    for entry in itertools.product(*data_arrays):
        sequence_data.append(entry)

    # Holds the current choices to choose from.  The data will come from
    # sequence_data above, but randomly shuffled.  Values are popped off the
    # end to keep things efficient.  It's possible to ask for more data than
    # the samples can give without repeats.  In that case, we'll reload
    # temp_data, randomly shuffle again, and start the process over until we've
    # delivered the number of desired results.
    temp_data = []
    # Build the lists
    for i in xrange(num_values):
        if len(temp_data) == 0:
            temp_data = sequence_data[:]
            random.shuffle(temp_data)
        yield temp_data.pop()

或使用for entry in random_models_avoid_repeats_generator(10000): # Do stuff... 手动迭代它。