Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 列表中的空格_Python_List - Fatal编程技术网

Python 列表中的空格

Python 列表中的空格,python,list,Python,List,我现在被这个问题困扰着。 我将首先解释我希望这个程序做什么 我有以下代码: rij4 = [] rij3 = [] rij2 = [] rij1 = list (range(1,16)) print("{}".format(rij1)) for x in range(0,14): cijfer = int((rij1[x] * rij1[x+1]) // (rij1[x+1] - rij1[x])) rij2.append(cijfer) print(rij2) for x

我现在被这个问题困扰着。 我将首先解释我希望这个程序做什么

我有以下代码:

rij4 = []
rij3 = []
rij2 = []
rij1 = list (range(1,16))
print("{}".format(rij1))

for x in range(0,14):
    cijfer = int((rij1[x] * rij1[x+1]) // (rij1[x+1] - rij1[x]))
    rij2.append(cijfer)
print(rij2)

for x in range(0,13):
    cijfer = int((rij2[x] * rij2[x+1]) // (rij2[x+1] - rij2[x]))
    rij3.append(cijfer)
print(rij3)
这将导致打印列表,但数字需要相互重叠。此外,还需要在打印中删除[]

例如,我希望打印以下内容:

1  2  3  4   5   6   7   8   9  10   11    12    13   14  15
2  6 12 20  30  42  56  72  90 110  132   156   182  210
3 12 30 60 105 168 252 360 495 660  858  1092  1365

非常感谢你的帮助

您可以稍微调整一下格式以实现您想要的内容。您可以使用以下代码将整数
x
填充为
'
字符:

"{: 3d}".format(x)
这通过在空格前加空格将
x
填充到3位。另外,要打印一个列表,您可以使用一个空格分隔
l
。join(l)。现在,如果将两者结合起来,您将得到:

print(" ".join(["{: 3d}".format(x) for x in rij3]))

这或多或少是您想要的(可能需要在需要换行符的地方添加换行符)

print(“.join([str(x)代表rij3中的x]))
insteadGood-response,+1。你能不能在格式模式上扩展一点,或者用两行文字来完成整个过程,以便将来的访问者更容易理解?@slezica我扩展了我的答案,使之更详细。