简体   繁体   English

遍历for循环内的字典

[英]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)
  • Every time you iterate, you copy the ENTIRE list of values, just to access a single element. 每次迭代时,您都复制整个值列表,以访问单个元素。
  • element is a key in the new_site dict. elementnew_site字典中的键。 There are ways to ask for the value associated with a key. 有一些方法可以要求与键关联的值。 Go over the tutorial and the documentation for 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(): ... 
  • When you pass 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? 那真的是你想要的吗? You need to call the function on an object to get its type. 您需要在对象上调用该函数以获取其类型。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM