首页 > 解决方案 > For循环在Python中打印变量

问题描述

我想在 Python 3 中打印变量 vector1 和 vector2,而不必手动编写打印代码。我怎样才能做到这一点?您可以在下面看到我尝试使用的代码。

vectorInput = input("Enter vectors values separated by ',' and vectors separated by ' ': ")

vector1,vector2 = vectorInput.split(" ")

for num in range(1,3):
    print({}.format('vector'+num))

谢谢你。

标签: pythonpython-3.x

解决方案


好吧,您可以直接使用推导式。

[print(i) for i in vectorInput.split(" ")]

或者使用list向量,因为它更适合您的使用模式,您可以稍后重用它。

vectors = vectorInput.split(" ")
[print(i) for i in vectors]

或与for

vectors = vectorInput.split(" ")
for i in vectors:
    print(i)

推荐阅读