0

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

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

4 Comments

Can you like make this easier? I don't know what enumerate and col are?
Sure, I added some notes. col is simply what I called the value of the element as I iterated through the list.
Is there a way to do this without enumerate? I know it makes sense but I'm not supposed to use that.
Does that new edit answer your question? I still don't really understand the original. The las few sentences are confusing.

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.