Difference between results (Python and Dictionaries)

Hi there,

I’m experiencing some troubles with dictionaries in python (i read that in dynamo there are some limitations to work with them in python) but I’m not sending a dictionary into the OUTPUT, so I think there is no error here.

The issue is that I developed the code in an online interpreter (https://repl.it/MB3v/14), and I’m getting this result for AUX:

image

but if I take the same code into a python node, i get this:

This is the code:

lineas = []
lineas.append(['c'  , 0   , 470 ,0])
lineas.append(['c'  , 255 , 410 ,70])
lineas.append(['c'  , 135   , 255 ,160])
lineas.append(['c'  , 410 , 470 ,160])
lineas.append(['c'  , 0 , 135 ,300])

# Lineas cargadas 
# Diccionario con la cantidad de segmentos por z
segmentosPiso = {}
for x in range(0,len(lineas)):
    segmentosPisoObjetoLocal = {}
    if lineas[x][3] in segmentosPiso.keys():
      segmentosPiso[lineas[x][3]] = segmentosPiso[lineas[x][3]] + 1
    else:
      segmentosPiso[lineas[x][3]] = 1


lineasCargadas = {}
contador = 0
iCoun = 0
AUX = []
for k,v in segmentosPiso.items():
    lineasADibujar = []
    if ((contador > 0) and (contador < len(segmentosPiso) - 1)):
        # print (k,v)
        ind = []
        for lline in range(0,len(lineas)):
          if (k == lineas[lline][3]):
            ind.append(lline)
        for c in ind:
            lineasCargadas[iCoun] = (c)
            iCoun +=1
        # print(lineasCargadas)
        for xline in range(1,len(lineas)):
          bFlag = False
          for xCar , xVal in  lineasCargadas.items():
            if((lineas[xline][1] == lineas[xVal][1]) and (lineas[xline][2] == lineas[xVal][2])):
              bFlag = True
          if bFlag == False:
            lineasADibujar.append(xline)
        AUX.append(lineasADibujar)
        # print(lineasADibujar)    
    #bflag = False
    contador += 1

I really don’t get where is the error or what am I missing here.

Thanks in advance!

Finnally we came with a solution:

lineas = {}
lineas[0] = ['c'  , 0   , 470 ,0]
lineas[1] = ['c'  , 255 , 410 ,70]
lineas[2] = ['c'  , 135 , 255 ,160]
lineas[3] = ['c'  , 410 , 470 ,160]
lineas[4] = ['c'  , 0   , 135 ,300]

# Lineas cargadas
# Diccionario con la cantidad de segmentos por z
lineas = PaqueteLimites
segmentosPiso = {}
for x in range(0,len(lineas)):
    segmentosPisoObjetoLocal = {}
    if lineas[x][3] in segmentosPiso.keys():
      segmentosPiso[lineas[x][3]] = segmentosPiso[lineas[x][3]] + 1
    else:
      segmentosPiso[lineas[x][3]] = 1

segPiso = []
for k,v in segmentosPiso.items():
    segPiso.append([k,v])

lineasCargadas = []
contador = 0
aux = []
for info in range(0,len(segPiso)):
    if ((contador > 0) and (contador < len(segmentosPiso) - 1)):
        lineasADibujar = []
        ind = []
        for lline in range(0,len(lineas)):
          if (segPiso[info][0] == lineas[lline][3]):
            ind.append(lline)
        for indiceCargado in ind:
            lineasCargadas.append(indiceCargado)
        for xline in range(1,len(lineas)):
          bFlag = False
          for  xVal in  range(0,len(lineasCargadas)):
            if((lineas[xline][1] == lineas[lineasCargadas[xVal]][1]) and (lineas[xline][2] == lineas[lineasCargadas[xVal]][2])):
              bFlag = True
          if bFlag == False:
            lineasADibujar.append(xline)
        aux.append(lineasADibujar)
    contador += 1

We changed dictionaries into lists and still got the same result. Then, we realized that segPiso wasn’t in the order wanted, so we added segPiso = sorted(segPiso) after it’s definition, and the problem was solved.

lineas = {}
lineas[0] = ['c'  , 0   , 470 ,0]
lineas[1] = ['c'  , 255 , 410 ,70]
lineas[2] = ['c'  , 135 , 255 ,160]
lineas[3] = ['c'  , 410 , 470 ,160]
lineas[4] = ['c'  , 0   , 135 ,300]

segPiso = []
for k,v in segmentosPiso.items():
segPiso.append([k,v])
segPiso = sorted(segPiso)

lineasCargadas = []
contador = 0
IndicesCurvas = []
for info in range(0,len(segPiso)):
if ((contador > 0) and (contador < len(segmentosPiso) - 1)):
    lineasADibujar = []
    ind = []
    for lline in range(0,len(lineas)):
      if (segPiso[info][0] == lineas[lline][3]):
        ind.append(lline)
    for indiceCargado in ind:
        lineasCargadas.append(indiceCargado)
    for xline in range(1,len(lineas)):
      bFlag = False
      for  xVal in  range(0,len(lineasCargadas)):
        if((lineas[xline][1] == lineas[lineasCargadas[xVal]][1]) and (lineas[xline][2] == lineas[lineasCargadas[xVal]][2])):
          bFlag = True
      if bFlag == False:
        lineasADibujar.append(xline)
    IndicesCurvas.append(lineasADibujar)
contador += 1

But still, don’t really know why the problem of the order or the dictionary not working. Any suggestions?

Thanks!

Python dictionary are unordered data types.
For a dictionary that keeps items ordered you need the OrderedDict from the collections module.

1 Like

Thanks @Gui_Talarico. I’ll keep this in mind!