从列表中每行打印单个变量

时间:2022-03-25 02:45:21

So I'm pretty new and am still learning but I can't find an answer anywhere..., that being said here is my question: I'm trying to generate output with multiple lists on different line and I want one at a time, I'll post my non-working code, and then a sample of code of one that works with just a single list:

所以我很新,我还在学习,但我无法在任何地方找到答案...,这里说的是我的问题:我正在尝试在不同的行上生成多个列表的输出,我想要一个时间,我将发布我的非工作代码,然后是一个只使用一个列表的代码示例:

Prints every variable repeatedly on each line the list is called:

在调用列表的每一行上重复打印每个变量:

networks = ["192.168.1.1 255.255.255.0", "192.168.2.1 255.255.255.0", "192.168.3.1 255.255.255.0"]
vlans = ["1001", "1002", "1003"]
name = ["MGMT", "DATA", "VOICE"]

for s in (networks, vlans, name):
    print "vlan %r" % (vlans)
    print "Name %r" % (name)
    print "int vlan %r" % (vlans)
    print " ip add %r" % (networks)

Generates desired output, placing variable sequentially one at a time:

生成所需的输出,一次一个地放置变量:

networks = ['192.168.1.1', '192.168.2.1', '192.168.3.1']

for address in networks:
    print "ip add %s" % address
    print "description I can't place multiple variables :'("

Any ideas would be greatly appreciated, ultimately the reason I want to format with list names is the ability to be able to use multiple lists on a single line in this fashion. Again thank you in advance.

任何想法都将受到高度赞赏,最终我想用列表名称格式化的原因是能够以这种方式在一行上使用多个列表。再次感谢你。

Edit: In advance, I understand doing separate for loops for each list, I want this to be done with the order of operations the print statements are made in.

编辑:事先,我知道为每个列表分别执行for循环,我希望这可以通过print语句的操作顺序来完成。

2 个解决方案

#1


networks = ["192.168.1.1 255.255.255.0", "192.168.2.1 255.255.255.0", "192.168.3.1 255.255.255.0"]
vlans = ["1001", "1002", "1003"]
names = ["MGMT", "DATA", "VOICE"]

for network, vlan, name in zip(networks, vlans, names):
    print "vlan %r" % (vlan)
    print "Name %r" % (name)
    print "int vlan %r" % (vlan)
    print " ip add %r" % (network)

P.S.

Regarding your code. for s in (list1, list2...) iterates the tuple of lists, not the the lists themselves, hence s is a list at each iteration. In fact, you don't even use s in your code. You refer to your master lists at each iteration, hence you get them printed entirely every single time.

关于你的代码。 for s in(list1,list2 ...)迭代列表的元组,而不是列表本身,因此s是每次迭代的列表。实际上,您甚至不在代码中使用s。您在每次迭代时都会引用主列表,因此您每次都会完全打印它们。

#2


You can use zip:

您可以使用zip:

for ip, vlan, n in zip(networks, vlans, name):
    print ip
    print vlan
    print n

#1


networks = ["192.168.1.1 255.255.255.0", "192.168.2.1 255.255.255.0", "192.168.3.1 255.255.255.0"]
vlans = ["1001", "1002", "1003"]
names = ["MGMT", "DATA", "VOICE"]

for network, vlan, name in zip(networks, vlans, names):
    print "vlan %r" % (vlan)
    print "Name %r" % (name)
    print "int vlan %r" % (vlan)
    print " ip add %r" % (network)

P.S.

Regarding your code. for s in (list1, list2...) iterates the tuple of lists, not the the lists themselves, hence s is a list at each iteration. In fact, you don't even use s in your code. You refer to your master lists at each iteration, hence you get them printed entirely every single time.

关于你的代码。 for s in(list1,list2 ...)迭代列表的元组,而不是列表本身,因此s是每次迭代的列表。实际上,您甚至不在代码中使用s。您在每次迭代时都会引用主列表,因此您每次都会完全打印它们。

#2


You can use zip:

您可以使用zip:

for ip, vlan, n in zip(networks, vlans, name):
    print ip
    print vlan
    print n