[英]Iterate through a dictionary inside a for loop
I'm trying to print the values of a dictionary inside a for loop, currently this is what I'm getting: 我正在尝试在for循环中打印字典的值,目前这就是我得到的:
Site 1
{'Port': '1/0/2', 'Link-Status': 'Up', 'Vlans': '81,1001,1101,1301,1303'}
{'Port': '1/0/4', 'Link-Status': 'Down', 'Vlans': '1007'}
{'Port': '1/0/11', 'Link-Status': 'Up', 'Vlans': '30,31,81,82'}
{'Port': '1/0/13', 'Link-Status': 'Up', 'Vlans': '30,31,81,82,1001,1101'}
{'Port': '1/0/14', 'Link-Status': 'Up', 'Vlans': '1007'}
Site 1
{'Port': '1/0/2', 'Link-Status': 'Up', 'Vlans': '82,1301,2001,2101'}
{'Port': '1/0/4', 'Link-Status': 'Down', 'Vlans': '2007'}
{'Port': '1/0/11', 'Link-Status': 'Up', 'Vlans': '30,31,81,82'}
{'Port': '1/0/13', 'Link-Status': 'Up', 'Vlans': '30,31,81,82,1301,2001,2101'}
{'Port': '1/0/14', 'Link-Status': 'Up', 'Vlans': '2007'}
Site 1
{'Port': '1/0/2', 'Link-Status': 'Up', 'Vlans': '1006,1102'}
{'Port': '1/0/4', 'Link-Status': 'Down', 'Vlans': '1007'}
{'Port': '1/0/11', 'Link-Status': 'Up', 'Vlans': '20,21'}
{'Port': '1/0/13', 'Link-Status': 'Up', 'Vlans': '20,21,1006,1102'}
{'Port': '1/0/14', 'Link-Status': 'Up', 'Vlans': '1007'}
Site 1
{'Port': '1/0/2', 'Link-Status': 'Down', 'Vlans': '2006,2102'}
{'Port': '1/0/4', 'Link-Status': 'Down', 'Vlans': '2007'}
{'Port': '1/0/11', 'Link-Status': 'Up', 'Vlans': '20,21'}
{'Port': '1/0/13', 'Link-Status': 'Up', 'Vlans': '20,21,2006,2102'}
{'Port': '1/0/14', 'Link-Status': 'Up', 'Vlans': '2007'}
I have tried indexing the values inside the for loop, but I haven't had luck with this: 我试图索引for循环中的值,但是我还没有走运:
def main():
input_site = new_site.keys()
print(new_site.values())
for element in input_site:
i=0
print(list(new_site.values())[i])
i+=1
processInput(element,type)
This is the content of my dictionary: 这是我的字典的内容:
print(new_site.values())
dict_values(['Site 1', 'Site 3', 'Site 7', 'Site 9'])
This is what I'm aiming for: 这是我的目标:
Site 1
{'Port': '1/0/2', 'Link-Status': 'Up', 'Vlans': '81,1001,1101,1301,1303'}
{'Port': '1/0/4', 'Link-Status': 'Down', 'Vlans': '1007'}
{'Port': '1/0/11', 'Link-Status': 'Up', 'Vlans': '30,31,81,82'}
{'Port': '1/0/13', 'Link-Status': 'Up', 'Vlans': '30,31,81,82,1001,1101'}
{'Port': '1/0/14', 'Link-Status': 'Up', 'Vlans': '1007'}
Site 3
{'Port': '1/0/2', 'Link-Status': 'Up', 'Vlans': '82,1301,2001,2101'}
{'Port': '1/0/4', 'Link-Status': 'Down', 'Vlans': '2007'}
{'Port': '1/0/11', 'Link-Status': 'Up', 'Vlans': '30,31,81,82'}
{'Port': '1/0/13', 'Link-Status': 'Up', 'Vlans': '30,31,81,82,1301,2001,2101'}
{'Port': '1/0/14', 'Link-Status': 'Up', 'Vlans': '2007'}
Site 7
{'Port': '1/0/2', 'Link-Status': 'Up', 'Vlans': '1006,1102'}
{'Port': '1/0/4', 'Link-Status': 'Down', 'Vlans': '1007'}
{'Port': '1/0/11', 'Link-Status': 'Up', 'Vlans': '20,21'}
{'Port': '1/0/13', 'Link-Status': 'Up', 'Vlans': '20,21,1006,1102'}
{'Port': '1/0/14', 'Link-Status': 'Up', 'Vlans': '1007'}
Site 9
{'Port': '1/0/2', 'Link-Status': 'Down', 'Vlans': '2006,2102'}
{'Port': '1/0/4', 'Link-Status': 'Down', 'Vlans': '2007'}
{'Port': '1/0/11', 'Link-Status': 'Up', 'Vlans': '20,21'}
{'Port': '1/0/13', 'Link-Status': 'Up', 'Vlans': '20,21,2006,2102'}
{'Port': '1/0/14', 'Link-Status': 'Up', 'Vlans': '2007'}
Any suggestions? 有什么建议么?
Inside your for-loop
you have the following lines 在
for-loop
您具有以下几行
for element in input_site:
i=0 # <---
print(list(new_site.values())[i])
i+=1
At each iteration you assign i to be 0, then print the i-th element of list(new_site.values())
and then increase its value by one. 在每次迭代中,将i赋值为0,然后打印
list(new_site.values())
的第i个元素,然后将其值增加1。
The same will happen for the next iterations. 下一次迭代也会发生同样的情况。
So, to fix it, i
should be moved outside the loop. 因此,要解决此问题,
i
应该移到循环之外。
Other than the i=0
problem, there are some major issues with your for-loop: 除了
i=0
问题,您的for循环还有一些主要问题:
for element in input_site:
print(list(new_site.values())[i])
i+=1
processInput(element,type)
element
is a key in the new_site
dict. element
是new_site
字典中的键。 There are ways to ask for the value associated with a key. dict
. dict
的教程和文档 。 When iterating over a dict, if you want both keys and values, you should use .items()
, not .keys()
. 遍历字典时,如果您既需要键
.items()
使用值,则应使用.items()
而不是.keys()
。 Sample code: 样例代码:
for key, value in d.items(): ...
type
to processInput
, you are passing the function named type
. type
传递给processInput
,就是传递名为type
的函数。 type
is a function that takes in an object and tells you what type it has. type
是一个函数,它接受一个对象并告诉您它具有什么类型。 However, when you pass type
, you are just passing the function itself. type
,您只是在传递函数本身。 Is that really what you want? I recommend reviewing the material, or finding a tutor to explain to you what each part of your code does. 我建议您复习材料,或找一位导师向您解释代码各部分的作用。 Prepare some questions beforehand.
事先准备一些问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.