consider this python list comprehension code in Rhino v.5 below:
for j in range(1,3):
globals()["objlist" + str(j)] = [rs.OrientObject(obj, [rs.PointCoordinates(x) for x in objlist[1:4]], [rs.PointCoordinates(x) for x in objlist[1+ j*3:4+ j*3]], 1) for obj in objlist]
as a lot of you will not know rhinoscriptsyntax commands which i'm calling here:
rs.OrientObject and rs.PointCoordinates
I hope you can read the rest syntax, as the problem is in the list comprehension not in rhinoscriptsyntax.
what i'm failing to get to work is the second 'j' inside the list slicing code of:
objlist[1+ j*3:4+ j*3]
I only get the results of this for j=1 but not for j=2 although i'm using a j in a loop in range(1,3)
is the 'for j in range(1,3)' not working at all in this case, is it working only for the left side of the '=' where i'm using it for the 'objlist + str(j)' or it shouldn't be used at all with list comprehension and I should find a way to rewrite everything with list comprehension?
p.s. even if I resolved the globals() issue with dictionaries, I would still need the j in order to slice the lists on different ranges on every iteration.
Well, that part is working fine:
>>> objlist = range(10)
>>> for j in range(1,3):
... print objlist[1+ j*3:4+ j*3]
...
[4, 5, 6]
[7, 8, 9]
Note that, due to the way you've written the loop, the case with j
equal to 1
will apply only to your variable objlist1
, and the case with j
equal to 2
will apply only to your variable objlist2
. Is that what's confusing you?
I think you should also refactor this, it's better to use an explicit loop than try to be clever with list comprehensions all the time if the comprehension is going to be such a long line. And, yes, you should be using a dictionary here rather than mucking with globals()
but I guess you already know that.