以n个Python的随机顺序倍数遍历列表

时间:2019-04-06 23:17:33

标签: python-3.x for-loop random

我有以下示例代码:

T(n) = sum_{p = 1}{n^2} sum_{q=1}{p} (1) = sum_{p=1}{n^2} (p) = 1 + 2 + 3 + ... + n^2 = n^2(n^2 + 1)/2 = Theta(n^4)

哪个给我输出:

l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

def batch_gen(data, batch_size):
    for i in range(0, len(data), batch_size):
        yield data[i:i+batch_size]
batch_size = randint(1,4)
n = 0
print("batch_size is {}".format(batch_size))
for i in batch_gen(l, batch_size):
    for x in i:
        print(x)
    print("loop {}".format(n))
    n += 1
    batch_size = randint(1,4)

这是我要查找的输出,但是总是在for循环之外,从batch_size = randint(1,4)设置batch_size。

我正在为循环的每次迭代寻找一个随机的batch_size,并且每个下一个循环都在最后一个循环停止直到随机长度列表完成的地方拾取。

任何帮助将不胜感激!

示例代码取自Iterate over a python sequence in multiples of n?

0 个答案:

没有答案