Sunisc
Sunisc

Reputation: 23

Excluding values in array - python

Ok so I have an array in python. This array holds indices to another array. I removed the indices I wanted to keep from this array.

stations = [1,2,3]

Let's call x the main array. It has 5 columns and I removed the 1st and 5th and put the rest in the array called stations.

I want to be able to create an if statement where the values from stations are excluded. So I'm just trying to find the number of instances (days) where the indices in the stations array are 0 and the other indices (0 and 4) are not 0.

How do I go about doing that? I have this so far, but it doesn't seem to be correct.

for j in range(len(x)):
    if  x[j,0] != 0 and x[j,4] != 0 and numpy.where(x[j,stations[0]:stations[len(stations)-1]]) == 0:
        days += 1
return days

Upvotes: 0

Views: 6087

Answers (1)

Antithesis
Antithesis

Reputation: 181

I don't think your problem statement is very clear, but if you want the x cols such that you exclude the indices contained in stations then do this.

excluded_station_x = [col for i, col in enumerate(x) if i not in stations]

This is a list comprehension, its a way for building a new list via transversing an iterable. Its the same as writing

excluded_station_x = []
for i, col in enumerate(x):
  if i not in stations:
    excluded_station_x.append(col)

enumerate() yields both the value and index of each element as we iterate through the list.

As requested, I will do it without enumerate. You could also just del each of the bad indices, although I dislike this because it mutates the original list.

for i in stations:
  del x[i]

Upvotes: 1

Related Questions