如何在python中逐行填充table?我试过追加但没有用
how to fill up a table row by row in python? I tried append but it didn't work
我想找出所有数字{0,1,2,3,4}的组合,并在table中打印出来,第一列是订单号,第二列是一个特定的组合。我想要的输出应采用以下形式:
1 (0,)
2 (1,)
... ...
6 (0,1)
... ...
我尝试了以下代码
import numpy as np
import itertools
rows=list(range(5))
combrows=[]
for k in range(1,5): #the number of rows k takes values from 1 to 5
for combo in itertools.combinations(rows,k):
combrows.append(combo)
ind=1
store=[]
for i in combrows:
store.append([[ind],[i]])
ind=ind+1
print(store)
但是生成的 table 是一条水平线,而不是具有两列的二维矩形 table。我该如何解决这个问题?谢谢!
这是一个非常简单的解决方案:
from itertools import combinations
numbers = list(range(5))
lst = []
for l in (combinations(numbers, r) for r in range(1, 5)):
lst.extend(l)
for i, j in enumerate(lst):
print(i+1, j)
enumerate
自动生成行号。
我想找出所有数字{0,1,2,3,4}的组合,并在table中打印出来,第一列是订单号,第二列是一个特定的组合。我想要的输出应采用以下形式:
1 (0,)
2 (1,)
... ...
6 (0,1)
... ...
我尝试了以下代码
import numpy as np
import itertools
rows=list(range(5))
combrows=[]
for k in range(1,5): #the number of rows k takes values from 1 to 5
for combo in itertools.combinations(rows,k):
combrows.append(combo)
ind=1
store=[]
for i in combrows:
store.append([[ind],[i]])
ind=ind+1
print(store)
但是生成的 table 是一条水平线,而不是具有两列的二维矩形 table。我该如何解决这个问题?谢谢!
这是一个非常简单的解决方案:
from itertools import combinations
numbers = list(range(5))
lst = []
for l in (combinations(numbers, r) for r in range(1, 5)):
lst.extend(l)
for i, j in enumerate(lst):
print(i+1, j)
enumerate
自动生成行号。