例如,如果我们输入数字7和3,我们得到:
Please enter list of lists length: 7
Please enter number of columns: 3
[[0,1,2],[3,4,5],[6,0,0]]
这可能是一种方法-(python3.x)考虑要在input2 x input2(2 D矩阵)中输出,第一个输入结束后的位置为零。
count = 0
input1 = int(input())
input2 = int(input())
arr = [['0' for i in range(input2)] for j in range(input2)]
for i in range(input2):
for j in range(input2):
if count < input1:
arr[i][j] = count
count +=1
else:
arr[i][j] = 0
print(arr)
这应该是您所需要的。
length = int(input('Please enter list of lists length'))
cols = int(input('Please enter number of columns'))
nums = list(range(length)) + [0] * cols
ls = [nums[k:k+cols] for k in range(0, length, cols)]
print(ls)
此表达式产生了我想要的内容:
return [[x if x < length else 0 for x in range(r,r+column)] for r in range(0,length,column)]