Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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
Python 在for循环中遍历字典_Python_Loops_Dictionary_Nested - Fatal编程技术网

Python 在for循环中遍历字典

Python 在for循环中遍历字典,python,loops,dictionary,nested,Python,Loops,Dictionary,Nested,我试图在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'} {

我试图在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'}
我曾尝试在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)
这是我字典的内容:

    print(new_site.values())
    dict_values(['Site 1', 'Site 3', 'Site 7', 'Site 9'])
这就是我的目标:

    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'}

有什么建议吗?

在循环的
中,您有以下几行

for element in input_site:
    i=0 # <---
    print(list(new_site.values())[i])
    i+=1
输入站点中元素的

i=0#除了
i=0
问题之外,for循环还有一些主要问题:

for element in input_site:
    print(list(new_site.values())[i])
    i+=1
    processInput(element,type)
  • 每次迭代时,复制整个值列表,只是为了访问单个元素
  • 元素
    新站点
    目录中的一个键。有几种方法可以请求与键关联的值。检查并输入
    命令
  • 在dict上迭代时,如果同时需要键和值,则应使用,而不是。示例代码:

    for key, value in d.items():
        ...
    
  • 当您将
    type
    传递给
    processInput
    时,您正在传递名为
    type
    的函数<代码>类型
是一个接收对象并告诉您其类型的函数。但是,当您传递
type
时,您只是传递函数本身。这真的是你想要的吗?您需要对对象调用函数以获取其类型
我建议您阅读这些材料,或者找一位导师向您解释代码的每一部分的作用。事先准备一些问题。

谢谢,我把I=0放在for循环之前。