Write sums element, insert them with Python into one list and to Excel

I managed to write a python script to sum element of a list, than
insert the sum as a line into the list.
I checked on Online tutor python execution, also Anaconda and it worked.
When I pasted this script into python node I get some errors.
I surely misplaced something there .


It maybe something simple , so far I have not found the solution yet.
While the purpose is somehow silly, I think I can use this solution for other purposes as well If I get it right!

Working versions on Revit 2017 and Dynamo 1.2.1 3083
Any hint would be appreciated
I upload also the overall graph, a red circle indicates where I inserted the python node.
I tried also to read some design script instructions, but as I understood I needed

to go into imperative mode for changing lists. It helped me to understand more about Dynamo but I could not make it functioning thus I went back to Python.ListSumsandInsertelement.py (3.3 KB)
I uploaded also the python definition “standalone”…

Please upload your Python code and/or dyn file and a sample rvt file to run it on.

Note that Dynamo only uses ironpyhon which is 2.7 so if you call modules or methods not in that version it won’t work.

The warning states “float is not iterable” meaning that Python is expecting an iterable list and is receiving a single float instead. Check to make sure all your lists are structured correctly.

Thanks. At the moment I had to leave the work as it is. I will come back. Since I cannot forward a file (it would be in Japanese btw) i will try to recreate the situation in a new file, maybe the Autodesk sample would work fine for this purpose.

1 Like

Hi @Uaifestival

There are many flavours of python like Cpython, Jython, Pypy, IronPython, Anaconda and many more .*ython. Each one has different syntax. Dynamo supports Iron Python you need to convert anaconda’s code to IronPython way. Cheers!

Thanks, is not python 2.7 Iron Python the same? To my understanding, when I set everything to 2.7 (tutor and Anaconda as well, they can work with different versions) I should be ok.
Anyway I will do more research. I think my problem was more on tweaking the
way data are entering into the python node as it is slightly different than giving a list
prepared for debugging. It is just an hypothesis.

I am back to solve the same issue. Today I had some time thus…I upload the definition, already
tried on pytho onlie tutor 2.7 and it worked fine.

Test_WritetoExcelAreasScheduleOrdered&SumInserted.dyn (45.3 KB)

I used the Revit sample file. I put the file on dropbox, here.
here below it is the content of the Python node. It is the first time I am pasting and copy here. …some readibility issues(?)

def flat (L):
“”"
T is a list of sublists
return a flattened list using a built-in method
“”"
from itertools import chain;
TL=list(chain.from_iterable(L))
return TL

def converfloat(A):
“”"
convert an element into float
“”"
#print type(A);
if type(A)== float:
return A
if type(A)== int:
return float(A)
try :
A=eval(A);
#print(A);
except ValueError:

    print "what is it?", A;
    
return A 

def fsomma(T,n):
“”"
T is a list sorted
by index 0
composed of
element of n,
(00, ‘trr’,…, 433214.656)
where the first is order, the last is the relevant value
fsomma
return a Dictionary
keys: the unique index at 0 of T
values: the sum of all T elements at
index n that have the same value at index 0
“”"
m=n ;
d={};
sommauguali=0.00;
#checking small size case(wrong input)

if len(T) < m-1:
    d[0]=T[0];
    return d

Tl=flat(T);
#print (Tl)
"""
starting the conversion into floats
for debugging
"""
j=m-1
while j<=(len(Tl)-m):
    try:
        Tl[j]=converfloat(Tl[j]);
        j=j+m
        # since the list is composed by every m ...(in the real case 3 parameters) 
    except ValueError:
        print ("Failed");
        break
# print (Tl)    

# case of one list with 1 element of the right size
if len(Tl) == m-1:
    d[Tl[0]]=Tl[m-1];
    return d
"""
starting to compile the dictionary
"""
#making a new list
Nl=[]
import copy
Nl = copy.deepcopy(Tl)
    
k=0;
#K = counter on index zero
while k< len(Tl):
    sw=True;
    temp=Tl[k];
    #JK counter on index 2
    jk=k;
    while jk < (len(Tl)-m):
        sw=True;
        while sw:
            if Tl[jk] == temp:
                sommauguali=sommauguali+Tl[jk+m-1]
                d[temp]=sommauguali
                jk=jk+m;
                if jk >= len(Tl):
                    Nl.append(Tl[k:jk]);
                    Nl.insert((jk+1),(" "," ",d[temp]));
                    k=jk;
                    sw=False;
                    break;
            else:
                Nl.append(Tl[k:jk]);
                Nl.insert((jk+1),(" "," ",d[temp]));
                temp=Tl[jk];
                sw=False;
                sommauguali=0.0;
                k=jk;
print (Tl)

Nl=flat(Nl);
print (Nl)
return Nl