Issue
I have matrix like this:
m1 =
[1, 3, 4, 2, 1, 1]
[1, 3, 5, 3, 3, 1]
[1, 2, 3, 1, 1, 0]
[1, 3, 7, 3, 1, 1]
I need to filter it tp get value 1 in the last column. So result should be like this:
filter_array =
[1, 3, 4, 2, 1, 1]
[1, 3, 5, 3, 3, 1]
[1, 3, 7, 3, 1, 1]
My current code is:
m1 = np.array ##see array above
filter_array = [ ] ## the filtered array will be stored here. raw by raw
for raw in m1:
if raw[-1] ==1:
filter_array = filter_array.append(raw[:])
My code gives me back an empty array. I've must missed something...
Solution
Simply do this:
filtered = m1[m1[:, -1] == 1]
Note: Your original code did not work because you were assigning the return value of append()
method to the filter_array
. Since the return value when append to a list is successful is None
, you lose your original list after reassigning it to None
.
To get your code working, simply do filter_array.append(row[:])
as this mutates the list in place. Simply appending row
also works.
Answered By - swag2198
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.