Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String str.format中的Python增量{#}_String_Python 3.x - Fatal编程技术网

String str.format中的Python增量{#}

String str.format中的Python增量{#},string,python-3.x,String,Python 3.x,我希望能够通过引用变量“count”来增加{}中的内容,因为我希望它在每次迭代时打印出列表“nodes”中的不同部分 count = 0 while len(nodes) > count: print("ltm node {} \n".format(*nodes)) count = count+1 print(" address {} \n".format(*nodes)) count = count+1 我无法从任何地方找到它 下面是完整的代码

我希望能够通过引用变量“count”来增加{}中的内容,因为我希望它在每次迭代时打印出列表“nodes”中的不同部分

count = 0

while len(nodes) > count:
    print("ltm node {} \n".format(*nodes))
    count = count+1
    print("    address {} \n".format(*nodes))
    count = count+1
我无法从任何地方找到它

下面是完整的代码

nodes = [] 

node_qty = int(input("Number Of Nodes: "))
count = 1
print("Enter the node details ")

while len(nodes) < node_qty*2:
    item1 = input("Enter Node %d Name: " % count)
    nodes.append(item1)
    item2 = input("Enter Node %d IP: " % count)
    nodes.append(item2)
    count = count+1

count = 0

print("\n"+"List of nodes:")

while len(nodes) > count:
    print("ltm node {0} \n".format(*nodes))
    count = count+1
    print("    address {1} \n".format(*nodes))
    count = count+1
目前它只显示索引0和1的详细信息,因为它是硬编码的,但是考虑到“count”变量的增量,我想在大括号中使用这个变量来代替固定的数字


(昨天才开始学习python,希望代码不要太草率:)

在每次输入迭代中,您都向列表中添加了2项,因此,保持逻辑不变,您还需要“以步长2对列表进行迭代”,并按照以下步骤为列表编制索引:

注意:当每个
打印str.format()
{0}
索引开始时

或作为一个打印语句:

print("ltm node {0} \n    address {1} \n".format(nodes[i], nodes[i+1]))   
输出:

List of nodes:
ltm node Server1 
    address 192.168.1.1 

ltm node Server2 
    address 160.10.10.1 

你到底想打印什么?也许您可以提供一个示例,说明您对给定输入所期望的输出,因为很难从您的问题中理解。
print("ltm node {0} \n    address {1} \n".format(nodes[i], nodes[i+1]))   
List of nodes:
ltm node Server1 
    address 192.168.1.1 

ltm node Server2 
    address 160.10.10.1