Python 创建';n';从多个列表中随机组合
上面的代码返回一个包含5^7=78125个列表的列表。是否有一种方法可以将a、b、c、d、e、f、g中的项目进行组合,可能是随机组合,以创建一个包含10000个列表的列表 您可以对每个数组进行随机采样并进行组合,尤其是在不需要保证特定组合不会出现多次的情况下:Python 创建';n';从多个列表中随机组合,python,combinations,itertools,Python,Combinations,Itertools,上面的代码返回一个包含5^7=78125个列表的列表。是否有一种方法可以将a、b、c、d、e、f、g中的项目进行组合,可能是随机组合,以创建一个包含10000个列表的列表 您可以对每个数组进行随机采样并进行组合,尤其是在不需要保证特定组合不会出现多次的情况下: 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] u
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
以下版本将避免重复,直到您请求的数据超过无需重复即可提供的数据:
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)
另外请注意,如果您使用yield
将其作为生成器,我们可以避免构建结果列表。但是,您还需要使用for
语句来使用结果:
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
您必须这样使用它:
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()
或者使用
next()
手动迭代它,非常感谢。是否有办法确保特定组合不会出现多次?提前谢谢。这取决于你愿意放弃什么。确保没有重复项,但提供随机样本涉及生成整个列表并删除随机样本以形成新列表,或者使用一些额外的逻辑来保持已交付内容的状态,以知道您已经用完了全部供应。你愿意用记忆来做这样的努力吗?如果是,那么我可以更新答案来做。是的,请。非常感谢你。
for entry in random_models_avoid_repeats_generator(10000):
# Do stuff...