IronPython BuiltinFunction and Dictionary

I have a dictionary that contains coatings and their area. I would like to add the areas if the coating is the same.

dictioonary

I tried to create a python to be able to manipulate the dictionary, but I was not successful. Also I tried to select only one key (from the first element of the list), but it returns Ironpython.runtime and I need it so that I can insert the data in revit and visualize it in dyanmo.
What can I do?I

Can you post the actual python code as well as a new screenshot showing the inputs you’re using? That will make it easier for us to replicate your work and see what you’re trying to do or where you may be making a mistake.

of course


import clr, sys, System

# adding Iron Python Path to System Variable.
# Needed to import any Iron Python module
ipython_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(ipython_path)

# <<< Iron Python Modules >>>
# BEGIN

# Import traceback module from Iron Python
import traceback
import csv
from clr import StrongBox
import random as rnd
import math

# END

# Import System Libraries
clr.AddReference("System.Core")
from System.Collections.Generic import List as SystemList
from System.Collections.Generic import Dictionary
from System import String, Object
dictionary = System.Collections.Generic.Dictionary[System.String,System.Object]()


# Import Linq
clr.ImportExtensions(System.Linq)

# Import Dynamo Library Nodes - Geometry
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript import Geometry as DynamoGeometry

# Import Dynamo Library Nodes - Core
clr.AddReference('DSCoreNodes')
from DSCore import List as DynamoList

# Import Dynamo Library Nodes - Core
clr.AddReference('DSCoreNodes')
from DSCore import Color as DynamoColor

# Import Dynamo Geometry Color
# https://forum.dynamobim.com/t/geometrycolor-bygeometrycolor-inside-python/52724
clr.AddReference('GeometryColor')
from Modifiers import GeometryColor as DynamoGeometryColorize

# Import Dynamo Library Nodes - Revit
clr.AddReference("RevitNodes")
import Revit as RevitNodes

# Import ToDSType(bool) extension method
clr.ImportExtensions(RevitNodes.Elements)
clr.ImportExtensions(RevitNodes.GeometryConversion)

# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

# Import Revit API
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

# Import Revit User Interface API
clr.AddReference("RevitAPIUI")
from Autodesk.Revit.UI import *

# Import Revit IFC API
# https://forum.dynamobim.com/t/ifcexportutils/4833/7?u=ricardo_freitas
clr.AddReference('RevitAPIIFC')
from Autodesk.Revit.DB.IFC import *

# Import Dynamo Services
import clr
clr.AddReference('DynamoServices')
from Dynamo import Events as DynamoEvents

from collections import namedtuple


# Active Dynamo Workspace Path
workspaceFullPath = DynamoEvents.ExecutionEvents.ActiveSession.CurrentWorkspacePath
workspacePath = '\\'.join(workspaceFullPath.split('\\')[0:-1])

doc =  DocumentManager.Instance.CurrentDBDocument

keys = IN[0]  #UnwrapElement(IN[0])
values = IN[1] #UnwrapElement(IN[1])
lista =  IN[2]  #Dictionary

z = dict(zip(keys,values))

#To print
t = []
resultado = {}
for i in zip(keys, values):
	t.append(i)

# here an error:  
# >>> File "<string>", line 107, in <module>
# TypeError: int() argument must be a string or a number, not 'builtin_function_or_method' <<<
for x in lista:
	for y in x:
		a = (x.keys).ToString
		b = (x.values).ToString
		resultado[a] = resultado.get(a, 0) + (b)

"""
>>>other attempts:

>1
for d in z:
	resultado[d.keys] = resultado.get(d.keys, 0) + int(d.values)

>2
for l in lista:
	for key, value in zip(keys,values):
		lista_dict[key] = key
		lista_dict[values]

>>>does not work
>3
dicionario = []
for k, v in zip(keys, values):
	for x, y in zip(k, v):
		meudict = {str(k) : v}
		dicionario.append(meudict)

>4
for key in meudict:
	dictionary.Add(key, meudict[key])

>5
teste = []
teste2 = []
for j in lista:
	z=j.values
	z = UnwrapElement(z)
	teste2.append(z)
	for i in j:
		teste.append(i)#.keys]
		#teste2 = UnwrapElement(j)

>6
Despesa = namedtuple('Dictionary',['values', 'keys'])
resultado = {}

for d in lista:
    resultado[d.keys] = resultado.get(d.keys, 0) + int(d.values) 
  
printa = []
for l in lista:
	printa.append(l.values)
	
nome = lista[0]
valor = lista[0].values

"""
# just to view
OUT = [values, keys,t, resultado, z, nome]
2 Likes

Hello,
You have missing parentheses
lista[0].keys(), lista[0].values()

If you’re not fully set on using Python for this you could look into the node List.GroupByKey after feeding your data into a Dictionary.Components node.

From then you could add the values using a Math.Sum.

1 Like