我正在尝试获取所有数字组合,但它有点复杂,所以我将通过示例展示:
假设我有一个类似 [5, 10, 12, 4, 6] 的列表,我想要 [1, 1, 1, 1, 1], [1, 2, 1, 1, 1], ... [ 5、10、12、4、6]
所以每个数字只会上升到它的最高点。我试过这个:
def listdir_nohidden(path):
for f in os.listdir(path):
if not f.startswith('.'):
yield f
def listdir(path):
return list(listdir_nohidden(path))
def listlen(path):
return len(list(listdir_nohidden(path)))
def randomiser(filename):
return random.randint(1,listlen(f'./{filename}'))
lengths = []
i = 0
while i < 5:
lengths.append(listlen(f'./{i}'))
i += 1
print(lengths) # ['5', '16', '16', '16', '6']
random_array = []
j = 0
while j < 30:
random_array.append([randomiser(0), randomiser(1), randomiser(2), randomiser(3), randomiser(4)])
j += 1
但是有重复的机会,它并不是真正随机的。我知道列表中的数字足够高,机会非常渺茫,但我实际上想生成很多这样的数字,所以重复的机会就会增加。