python - 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))
谢谢你。
解决方案
好吧,您可以直接使用推导式。
[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)
推荐阅读
- python - 导入数据类型为浮点数和索引类型为字符串的 .csv
- amazon-web-services - 从 jenkins 动态地将容器图像值传递给 AWS cloudformation 模板
- python-3.x - 在 Django Rest Framework 中生成二维码并将二维码图像保存到 AWS S3
- excel - excel可以自动加减法吗
- flutter - 我应该如何在颤振应用程序中管理登录页面
- scala - 是否可以对返回 ZIO 的方法进行尾递归?
- c# - 任务锁定 MVVM WPF 项目上的 UI
- python - How to setattr to instance such that self is passed?
- swift - 在 SwiftUI 中用另一个 @State 属性值初始化 @State 属性
- apache-kafka - 卡夫卡生产者处理失去与经纪人的联系