首页 > 解决方案 > 在python中的循环内打印

问题描述

我有两个列表,例如:

list1=["read1","read2","read3"]
list2=["read4","read5","read6"]

为了生成脚本,我需要打印它们,例如:

Programm -h -y -1 $read1,$read2,$read3 -2 $read4,$read5,$read6

我试过了:

print("Programm -h -y -1 "+str([print("$"+i,",",end='') for i in list1]))+" -2" + +str([print("$"+i,",",end='') for i in list2])))

但它确实是这样工作的,有人有想法吗?

标签: python

解决方案


不确定其背后的意图,但它可能很简单:

List1 = ['read1', 'read2', 'read3']
List2 = ['read4', 'read5', 'read6']

List1 = ["$"+(i) for i in List1]
List2 = ["$"+(i) for i in List2]

print('Programm -h -y -1', end=" ")
print(",".join(List1) + " -2 " + ",".join(List2))

编辑

甚至更好,谢谢@Matt B。

List1 = ['read1', 'read2', 'read3']
List2 = ['read4', 'read5', 'read6']

print("Programm -h -y -1 " + "$" + ",$".join(List1) + " -2 $" + ",$".join(List2))

输出

Programm -h -y -1 $read1,$read2,$read3 -2 $read4,$read5,$read6

推荐阅读