0

Following the previous question as follows Comparing list elements with a column in numpy array, I have implemented the following code to compare the list elements with array.

list=['M', 'R', 'W']
array=[['M',360.0, 360.0],['R', 135.9, 360.0],['W', 101.4, -125.4], ['Y', 115.8, -160.4],['Y', 115.8, -160.4],['W', 101.4, -125.4]]

new_array = [x for x in array if x[0] in list]
print(new_array)

The output is as below.

new_array=[['M', 360.0, 360.0], ['R', 135.9, 360.0], ['W', 101.4, -125.4], ['W', 101.4, -125.4]]

Here we see that "W"is repeated twice.

How can I take the only the top matching elements in the list and ignore the others if it is present in list.

So the output should be like as follows.

new_array=[['M', 360.0, 360.0], ['R', 135.9, 360.0], ['W', 101.4, -125.4]]

1 Answer 1

1

You can using the index which will return the 1st match

l1=[x[0] for x in array]
[array[y] for y in [l1.index(x) for x in l]]
[['M', 360.0, 360.0], ['R', 135.9, 360.0], ['W', 101.4, -125.4]]
Sign up to request clarification or add additional context in comments.

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.