0

consider the following text in rhino python where arrPts is a list of points

start_point = rs.GetPoint()
arrPts0 = []
arrPts0 = rs.GetPoints()

arrPts=[]
for ind, pt in enumerate(arrPts0):
    arrPts.append( [pt,ind, 'angle','vector',0] )

then I run the arrPts through a function (def) which replaces 'angle' with actual strings of angles (e.g. '>45') and then I want to run through the following code which ends up calling another separate function (def) called 'add_Vectors':

Vectors = []
newPts = []
for pt in arrPts:
    if '>45' or '<45' in pt[2]:
        newPts.append(pt)

Vectors.append(add_Vectors(newPts, start_point))

how can I rewrite the above part with list comprehension so that it runs only when the condition is met instead of creating a new list (newPts) but working always on the main list (arrPts), if there's a function (def) in the way ?

something like:

Vectors.append(add_Vectors(newPts, start_point) ----> for '>45' in pt[2] 

thanks

Pav

1h later...... tried this but doesn't seem to work:

newPts = []
Vectors = add_Vectors([newPts for pt in arrPts if '<45' in pt[2]], start_point)

1 Answer 1

1

and I think I found the correct syntax for just 1 line !

Vectors = add_Vectors([pt for pt in arrPts if '<45' in pt[2]], start_point)
Sign up to request clarification or add additional context in comments.

1 Comment

If you switch from [] to () you'll have a generator expression, which won't have to generate the whole list before add_Vectors is called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.