[英]Nested for loop + formatting multiple lists
Here is the code so far 这是到目前为止的代码
Notes regarding the variables: 有关变量的注意事项:
'ports'
- there could be any number of ports in the 'ports' list 'ports'
-'端口'列表中可以有任意数量的端口
'nodes_u'
this will contain any number of server names and IP address pairs 'nodes_u'
它将包含任意数量的服务器名称和IP地址对
'serv_name_u'
could be anything but is static once defined 'serv_name_u'
可以是任何东西,但一旦定义即为静态
'port_qty'
is equal to the number of ports listed in 'ports' 'port_qty'
等于“端口”中列出的端口数
ports = ['443', '80']
nodes_u = ['SERVER1', '10.0.0.1', 'SERVER2', '10.0.0.2']
serv_name_u = "TESTING"
port_qty = 2
for i in range(0, len(ports)):
print("ltm pool "+serv_name_u+"_{0}_pool {{ \n members {{"
.format(ports[i]))
for i in range(0, len(nodes_u), 2):
print(" {0} {{ \n address {1} \n }}"
.format(nodes_u[i], nodes_u[i+1]))
Here is an example of the output 这是输出示例
ltm pool TESTING_443_pool {
members {
SERVER1 {
address 10.0.0.1
}
SERVER2 {
address 10.0.0.2
}
ltm pool TESTING_80_pool {
members {
SERVER1 {
address 10.0.0.1
}
SERVER2 {
address 10.0.0.2
}
My question or what I am trying to figure out is how I can get the port number displayed after the server name in the pool section only! 我的问题或试图解决的是如何仅在池部分中获得服务器名称之后显示的端口号! so the output is like this eg
所以输出像这样
ltm pool TESTING_443_pool {
members {
SERVER1:443 { <<<Port Number Here!
address 10.0.0.1
}
SERVER2:443 { <<<Port Number Here!
address 10.0.0.2
}
ltm pool TESTING_80_pool {
members {
SERVER1:80 { <<<Port Number Here!
address 10.0.0.1
}
SERVER2:80 { <<<Port Number Here!
address 10.0.0.2
}
Every time I try something I just end up with both ports listed or the iteration jumps past the index range for the 'ports' list. 每次尝试执行某项操作时,我都会最终列出两个端口,否则迭代会超出“端口”列表的索引范围。 Also if there is a better way to capture the input or generate the output I am all ears!
同样,如果有更好的方法来捕获输入或生成输出,我也很高兴!
Remove the nested for
-loop and just include both print statements in the outer for
-loop, with the port
number inserted at index :{1}
: 删除嵌套的
for
循环,只将两个print语句都包含在for
循环的外部, port
号插入到索引:{1}
:
for i in range(0, len(ports)):
print("ltm pool "+serv_name_u+"_{0}_pool {{ \n members {{"
.format(ports[i]))
# for i in range(0, len(nodes_u), 2):
print(" {0}:{1} {{ \n address {2} \n }}"
.format(nodes_u[i], ports[i], nodes_u[i+1]))
Output: 输出:
List of nodes:
ltm node SERVER1 {
address 10.0.0.1
}
ltm node SERVER2 {
address 10.0.0.2
}
ltm pool TESTING_443_pool {
members {
SERVER1:443 {
address 10.0.0.1
}
ltm pool TESTING_80_pool {
members {
10.0.0.1:80 {
address SERVER2
}
Attempt #2 : Using different incrementer names ( i
and j
) in the nested for
-loops to differentiate between the two lists may get the desired output: 尝试#2 :在嵌套的
for
循环中使用不同的增量器名称( i
和j
)来区分两个列表可能会获得所需的输出:
for i in range(0, len(ports)):
print("ltm pool "+serv_name_u+"_{0}_pool {{ \n members {{"
.format(ports[i]))
for j in range(0, len(nodes_u), 2):
print(" {0}:{1} {{ \n address {2} \n }}"
.format(nodes_u[j], ports[i], nodes_u[j+1]))
Output: 输出:
ltm pool TESTING_443_pool {
members {
SERVER1:443 {
address 10.0.0.1
}
SERVER2:443 {
address 10.0.0.2
}
ltm pool TESTING_80_pool {
members {
SERVER1:80 {
address 10.0.0.1
}
SERVER2:80 {
address 10.0.0.2
}
As for a better way to capture the input or generate the output, consider using a different data structure for your inputs, like a dictionary with the keys
as name
, and port
, and values
as their respective values
. 为了更好地捕获输入或生成输出,请考虑对输入使用不同的数据结构,例如以
keys
为name
和port
以及values
分别为values
的字典 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.