1

I'm using Python 2.7 and couldn't find an answer to my question.

I have 3 lists (x and y coordinantes and the radius of detected circles):

XSum = [1316, 1324, 1330, 1344, 1356, 1344, 1356, 1308, 1322, 1310, 1336, 1934]
YSum = [960,  952,  972,  954,  964,  970,  948,  928,  940,  950,  984,  1080]
RSum = [379,  375,  355,  352,  340,  347,  338,  348,  361,  342,  342,  286]

I have to find elements in XSum which are not between 675 and 1350 (Index 4, 6 and 11). Elements with the index have to be removed from all three lists.

XSum = [1316, 1324, 1330, 1344, **1356**, 1344, **1356**, 1308, 1322, 1310, 1336, **1934**]
YSum = [960,  952,  972,  954, **964**,  970,  **948**,  928,  940,  950,  984,  **1080**]
RSum = [379,  375,  355,  352,  **340**,  347,  **338**,  348,  361,  342,  342,  **286**]

After first removal of index which are out of range:

XSum2 = [1316, 1324, 1330, 1344, 1344, 1308, 1322, 1310, 1336]
YSum2 = [960,  952,  972,  954,  970,  928,  940,  950,  984]
RSum2 = [379,  375,  355,  352,  347,  348,  361,  342,  342]

The next step is to find elements in YSum which are not between 940 and 980 and removed the detected index from all three lists (Index 9).

XSum2 = [1316, 1324, 1330, 1344, 1344, 1308, 1322, 1310, **1336**]
YSum2 = [960,  952,  972,  954,  970,  928,  940,  950,  **984**]
RSum2 = [379,  375,  355,  352,  347,  348,  361,  342,  **342**]

The resulting list should look like:

XSum_Result = [1316, 1324, 1330, 1344, 1344, 1308, 1322, 1310]
YSum_Result = [960,  952,  972,  954,  970,  928,  940,  950]
RSum_Result = [379,  375,  355,  352,  347,  348,  361,  342]

I think it should be an easy task, but I just couldn't figure it out how to remove by index from the other lists.

thank you all for the help

3
  • 1
    This is why it's generally better to make a single list of tuples or dictionaries, rather than having separate lists of each attribute. Commented Mar 24, 2017 at 15:51
  • 1
    You can do this type of filtering with a couple list comprehensions. Commented Mar 24, 2017 at 15:55
  • Umm... you know you're not dropping Y with a value of 928 there, right? Commented Mar 24, 2017 at 16:10

2 Answers 2

3

You can group them together using zip and perform your logic to subset the candidates, then zip again (which inverses the original zip) and unpack into lists, eg:

x, y, r = (list(el) for el in zip(*((a, b, c) for a, b, c in zip(XSum, YSum, RSum) if 675 <= a <= 1350 and 940 <= b <= 980)))
Sign up to request clarification or add additional context in comments.

4 Comments

This is good. You can even substitute the conditional expression with range itself, e.g. if a in range(675, 1351) and b in range(940, 981)
@pylang that's okay in Python 3 but on Python 2 it will build a list and run a slow O(N) containership test
Agreed. On another note, compared to the expected results, 1308, 928, 348 are missing from x, y, r respectively.
@pylang I think that's the OP's example output gone astray as 928 for Y doesn't meet the at least 940 criteria
1

You should have post a piece or code to help us know how to help you.

Here is a very basic idea, assuming that all list are sorted to match each other (I mean in index order) :

a = [1, 2, 3]
b = ['a', 'b', 'c']
for i in range(0, len(a)):
    if a[i] == 2:
        b.pop(i)
        break
print(b)

It should print out

['a', 'b']

Now, you can imagine this mechanism nested into a class holding an array of arrays well managed by an insert and a pop out methods and it becomes as easy as abc.

Comments

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.