Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

Removing ndarray from list in python

Very strange issue, giving you details for you to reproduce.

I have an ndarray x = [[ 58.0376135 ], [4739.44845915]] which is defined as ndarray size (2,1)

I also have this list

lst = [array([[11120.19965669],[ 1036.7331153 ]]), 
       array([[  58.0376135 ],[4739.44845915]]), 
       array([[ 766.38433838],[5524.3418457 ]])]

list of (2,1) ndarrays. As you can see, x == lst[1].

However, when I write lst.remove(x) or x in lst I get value error that the truth value of an array with more than one element is ambiguous. Strangely, on other examples it does work.

How can I make it work here too?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is a way to do it:

[arr for arr in lst if not np.all(x==arr)]

Since x is multidimensional you need to use np.all() instead of element-wise comparisons like x in arr


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...