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]]
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]]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.