Hi,
I could use some help with a piece of Python that I wrote.
Code is shared below.
The goal of the code is not important right now. The issue is the error message I get.
I find it strange because the code works fine inside other Python programs that I use, like QPython on my phone, or Python IDLE on my PC.
Only, inside Dynamo, a warning pops up: 'expected index, got float".
I cannot understand this different behaviour inside Dynamo.
Do you guys understand?
How can i solve the error?
Many thanks in advance.
#goal:
#input: list of elements with coordinates (list1)
#output: sorted elements list (list2)
#create 16 xy coordinates (a hexagon pattern)
#ultimately, xycoords will come from input
xylist = [[0,2],[0,3],[0,4],
[1.22,1.33],[1.44,5.55],
[2,0],[2,6],
[3,0],[3,6],
[4,0],[4,6],
[5.66,1.77],[5.88,5.99],
[6,2],[6,3],[6,4]]
#create a fictive element list
elemlist = list(range(len(xylist)))
list1 = list(zip(elemlist,xylist))
firstitem = list1[14]
list2 = [firstitem]
ycoords = sorted(list(set([x[1][1] for x in list1])))
xcoords = sorted(list(set([x[1][0] for x in list1])))
#list comprehension
def down1():
filtered=[]
for item in list1:
if item[1][0] == max(xcoords) and item[1][1]<firstitem[1][1]:
filtered.append(item)
filtered.sort(reverse = True,
key = lambda x: x[1])
for item in filtered:
list2.append(item)
def down2():
filtered = []
for item in list1:
if item[1][0] == max(xcoords) and item[1][1]>firstitem[1][1]:
filtered.append(item)
filtered.sort(reverse = True,
key = lambda x: x[1])
for item in filtered:
list2.append(item)
def left():
filtered = []
for item in list1:
if item[1][1] == min(ycoords):
filtered.append(item)
filtered.sort(reverse = True,
key = lambda x: x[1])
for item in filtered:
list2.append(item)
def up():
filtered = []
for item in list1:
if item[1][0] == min(xcoords):
filtered.append(item)
filtered.sort(key = lambda x: x[1])
for item in filtered:
list2.append(item)
def right():
filtered = []
for item in list1:
if item[1][1] == max(ycoords):
filtered.append(item)
filtered.sort(key = lambda x: x[1])
for item in filtered:
list2.append(item)
def downleft():
for item in list1:
if (
item[1][1] < list2[-1][1][1] and
item[1][0] < list2[-1][1][0] and
item[1][1] != min(ycoords) and
xcoords.index(item[1][0]) > xcoords[round(len(xcoords)/2)]):
list2.append(item)
def leftup():
for item in list1:
if (
item[1][1] > list2[-1][1][1] and
item[1][0] < list2[-1][1][0] and
item[1][0] != min(xcoords) and
ycoords.index(item[1][1]) < ycoords[round(len(ycoords)/2)]):
list2.append(item)
def upright():
for item in list1:
if (
item[1][1] > list2[-1][1][1] and
item[1][0] > list2[-1][1][0] and
item[1][1] != max(ycoords) and
xcoords.index(item[1][0]) < xcoords[round(len(xcoords)/2)]):
list2.append(item)
def rightdown():
for item in list1:
if (
item[1][1] < list2[-1][1][1] and
item[1][0] > list2[-1][1][0] and
item[1][0] != max(xcoords) and
ycoords.index(item[1][1]) > ycoords[round(len(ycoords)/2)]):
list2.append(item)
down1()
downleft()
left()
leftup()
up()
upright()
right()
rightdown()
down2()
print (list2)