0
a=[[(1,2),(7,-5),(7,4)],[(5,6),(7,2)],[(8,2),(20,7),(1,4)]]

A nested list of coordinates are as given in a.

For example (1,2) refers to x,y coordinates.

Imposing condition that x,y> 0 & <10 and deleting those points.

for x in a:
    for y in x:
        for point in y:
            if point<=0 or point>=10:
                a.remove(x)

Expected result a=[[(5,6),(7,2)]] This is the error I get:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
1
  • 3
    That error message doesn't match that code; that's a numpy error message, and you have no use of numpy arrays in that code. Commented Apr 23, 2015 at 13:28

2 Answers 2

3

Try this list comprehesion.

>>> a = [[(1,2),(7,-5),(7,4)], [(5,6),(7,2)], [(8,2),(20,7),(1,4)]]
>>> [l for l in a if all((0<x<10 and 0<y<10) for x,y in l)]
[[(5, 6), (7, 2)]]
Sign up to request clarification or add additional context in comments.

Comments

1

The following snippet will print [[(5, 6), (7, 2)]]:

a=[[(1,2),(7,-5),(7,4)],[(5,6),(7,2)],[(8,2),(20,7),(1,4)]]

def f(sub):
  return all(map(lambda (x,y): (0 < x < 10 and 0 < y < 10), sub))

a = filter(f, a)
print a

6 Comments

I'm sorry, I hadn't read the question (or the input rather) carefully enough. I've updated my answer.
Better now :) You can make your f function quite a bit shorter by having both sides of the range, for instance 0 < point[0] < 10
That's still wrong. The output should be` [[(5,6),(7,2)]]` that being the only sublist for which all points meet the condition.
@mhawke Nice catch, totally missed that in the question.
Once again, i had not read the question carefully enough. Thanks, @mhawke, re-updated.
|

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.