Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/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
Python 在for循环期间创建嵌套字典_Python_Python 2.7_Ironpython_Revit Api_Revitpythonshell - Fatal编程技术网

Python 在for循环期间创建嵌套字典

Python 在for循环期间创建嵌套字典,python,python-2.7,ironpython,revit-api,revitpythonshell,Python,Python 2.7,Ironpython,Revit Api,Revitpythonshell,我已经在for循环中创建了一个列表,它运行得很好,但是我想创建一个字典 from System.Collections.Generic import List #Collector viewPorts = list(FilteredElementCollector(doc).OfClass(Viewport)) #create a dictionary viewPortDict = {} #add Sheet Number, View Name and boxoutline to dict

我已经在for循环中创建了一个列表,它运行得很好,但是我想创建一个字典

from System.Collections.Generic import List

#Collector
viewPorts = list(FilteredElementCollector(doc).OfClass(Viewport))

#create a dictionary
viewPortDict = {}

#add Sheet Number, View Name and boxoutline to dictionary
for vp in viewPorts:
    sheet = doc.GetElement(vp.SheetId)
    view = doc.GetElement(vp.ViewId)
    vbox = vp.GetBoxOutline()
    viewPortDict = {view.ViewName : {'sheetNum': sheet.SheetNumber, 'viewBox' : vbox}}

print(viewPortDict)
其输出如下所示:

{'STEEL NOTES': {'viewBox': <Autodesk.Revit.DB.Outline object at 0x000000000000065A [Autodesk.Revit.DB.Outline]>, 'sheetNum': 'A0.07'}}
但我总是有同样的问题,我不能迭代所有元素。为了充分披露,我成功地创建了一个列表。这就是它的样子

from System.Collections.Generic import List

#Collector
viewPorts = list(FilteredElementCollector(doc).OfClass(Viewport))

#create a dictionary
viewPortList = []

#add Sheet Number, View Name and boxoutline to dictionary
for vp in viewPorts:
    sheet = doc.GetElement(vp.SheetId)
    view = doc.GetElement(vp.ViewId)
    vbox = vp.GetBoxOutline()
    viewPortList.append([sheet.SheetNumber, view.ViewName, vbox])

print(viewPortList)
它可以很好地工作并打印以下内容(仅是长列表的一部分)

[['A0.01','适用规范',]['A0.02'等]

但是我想要一个字典。任何帮助都将不胜感激。谢谢!

在您的列表示例中,您将
附加到列表中。在您的字典示例中,您每次都创建一个新字典(从而从循环的上一次迭代中删除数据)。您也可以通过只分配给现有字典中的特定键来执行附加操作

viewPortDict[view.ViewName] = {'sheetNum': sheet.SheetNumber, 'viewBox' : vbox}
[['A0.01', 'APPLICABLE CODES', <Autodesk.Revit.DB.Outline object at 0x000000000000060D [Autodesk.Revit.DB.Outline]>], ['A0.02', etc.]
viewPortDict[view.ViewName] = {'sheetNum': sheet.SheetNumber, 'viewBox' : vbox}