algorithmic modeling for Rhino
Hello everyone,
Does anyone knows how to make a mass addition for items inside a tuple?
In red is the error message and the lines related
Runtime error (TypeErrorException): unsupported operand type(s) for +=: 'int' and 'tuple'
Traceback:
line 64, in script
import rhinoscriptsyntax as rs
import Rhino.Geometry as geo
import math as mt
import random
import copy
list = zip(Pts,En)
a = []
No_columnas = int(div+1)
No_filas = int(length/No_columnas)
for i in range (No_filas):
a.append(list[i*No_filas:(i+1)*No_filas])
# PARTE 1.1
# a[fila][columna][item]
a2 = copy.deepcopy(a)
fila = 0
while fila < No_filas:
for columna in range(No_columnas):
print '-----------------------------'
print'Fila=',fila
print'Columna=',columna
print '-----------------------------'
C = a[fila][columna]
if fila == 0:
a2[fila][columna] = C
else:
Cijk = []
if columna > 0:
Cijk.append(a2[fila-1][columna-1])
if columna < No_columnas-1:
Cijk.append(a2[fila-1][columna+1])
Cijk.append(a2[fila-1][columna])
print 'Cijk=', Cijk
M = max(Cijk)
print 'MAXIMO=', M
a2[fila][columna] = C + M
fila = fila+1
## PARTE 2.1_
fila=0
columna=0
SEAMS = []
seam = []
while columna < No_columnas:
carga = a2[fila][columna]
seam.append((carga,columna,fila))
while fila < No_filas-1:
cargas_ijk = []
if columna > 0:
cargas_ijk.append((a2[fila+1][columna-1],columna-1,fila+1))
if columna < No_columnas-1:
cargas_ijk.append((a2[fila+1][columna+1],columna+1,fila+1))
cargas_ijk.append((a2[fila+1][columna],columna,fila+1))
carga,columna,fila = max(cargas_ijk)
seam.append((carga,columna,fila))
Mass_add = 0
for i,v in enumerate(seam):
Mass_add += v[0]
seam.append(Mass_add)
SEAMS.append(seam)
seam = []
fila = 0
columna = len(SEAMS)
Tags:
Hi emeceo,
Use "sum" function to calculate any python tuple or list mass addition:
exampleTuple = (0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0)
print sum(exampleTuple) # 45
Hi djordje many thanks for the reply.
But what you suggested works for a mass addition of all the elements inside a tuple.
I want to mass add all the [x]items of varies tuples in a list.
Based on what you send I resolved this:
----------------------------------------------------------------
exampleList =[(0,1),(2,3),(4,5),(6,7),(8,9)]
a = []
for i in range(len(exampleList)):
a.append(exampleList[i][0])
print sum(a)
#20
----------------------------------------------------------------
Hi emeceo,
You can use list comprehensions to calculate the sum of each python list tuple in one line:
exampleList =[(0,1),(2,3),(4,5),(6,7),(8,9)]
sumTuplesL = [sum(item) for item in exampleList]
print sumTuplesL # [1, 5, 9, 13, 17]
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by