哪个循环可以用来继续 while 循环?
Which loop can be used for continuing while loop?
我想知道如何使用循环来压缩这段代码。它是 (i) 变量定义 (ii) while 循环条件 ...(iii) 变量增量
的重复序列
list_main = []
x1 = 1
while x1<= n:
x2 = x1
while x2 <= n:
x3 = x2
while x3 <= n:
#continue the sequence: x(i) = x(i-1) ; while x(i) <= n:
list_ = [x1,x2,...]
list_main.append(list_)
x3 = x3 + 1
x2 = x2 + 1
x1 = x1 + 1
我想要这样的东西:
max_x = 10 #maximum number of 'x's
#code
list_ = [x1,x2,x3,x4,x5,x6,x7,x8,x9,x10]
编辑
如果“x”的数量是 2,n 是 5,那么我想要的输出,即 list_main 是
list_main = [[1,1],[1,2],[1,3],[1,4],[1,5],[2,2],[2,3],[2,4],[2,5],[3,3],[3,4],[3,5],[4,4],[4,5],[5,5]]
如果'x'的个数是3,n是3,那么我想要的输出是
list_main = [[1,1,1],[1,1,2],[1,1,3],[1,2,2],[1,2,3],[2,2,2],[2,2,3],[2,3,3],[3,3,3]]
正如中指出的,您的问题可以这样解决:
max_x = 10
n = 5
values = []
i = 1
while i <= n:
list_values.append(i)
i = i + 1
count = [max_x] * n
list_main = list(unique_combinations_from_value_counts(values,count,max_x))
我想知道如何使用循环来压缩这段代码。它是 (i) 变量定义 (ii) while 循环条件 ...(iii) 变量增量
的重复序列list_main = []
x1 = 1
while x1<= n:
x2 = x1
while x2 <= n:
x3 = x2
while x3 <= n:
#continue the sequence: x(i) = x(i-1) ; while x(i) <= n:
list_ = [x1,x2,...]
list_main.append(list_)
x3 = x3 + 1
x2 = x2 + 1
x1 = x1 + 1
我想要这样的东西:
max_x = 10 #maximum number of 'x's
#code
list_ = [x1,x2,x3,x4,x5,x6,x7,x8,x9,x10]
编辑
如果“x”的数量是 2,n 是 5,那么我想要的输出,即 list_main 是
list_main = [[1,1],[1,2],[1,3],[1,4],[1,5],[2,2],[2,3],[2,4],[2,5],[3,3],[3,4],[3,5],[4,4],[4,5],[5,5]]
如果'x'的个数是3,n是3,那么我想要的输出是
list_main = [[1,1,1],[1,1,2],[1,1,3],[1,2,2],[1,2,3],[2,2,2],[2,2,3],[2,3,3],[3,3,3]]
正如
max_x = 10
n = 5
values = []
i = 1
while i <= n:
list_values.append(i)
i = i + 1
count = [max_x] * n
list_main = list(unique_combinations_from_value_counts(values,count,max_x))