0

This toy example kind of represents a problem I have parsing data from a simulation package. I want to loop through a tuple and if a particular condition is met break the loop and return the time step that corresponds with that particular row. In the following example index 0 represents these time steps.

from random import randint

t = list(range(1,11))
a = [randint(1, 10) for i in range(0, 10)]
b = [randint(1, 10) for i in range(0, 10)]
c = [randint(1, 10) for i in range(0, 10)]
d = [randint(1, 10) for i in range(0, 10)]
e = [randint(1, 10) for i in range(0, 10)]

for i in zip(t,a,b,c,d,e):
    print (i)

(1, 8, 5, 5, 3, 4)
(2, 2, 9, 5, 9, 1)
(3, 9, 3, 1, 6, 3)
(4, 9, 6, 7, 8, 3)
(5, 2, 7, 6, 8, 10)
(6, 6, 6, 8, 9, 6)
(7, 1, 8, 1, 8, 3)
(8, 1, 1, 1, 7, 5)
(9, 6, 10, 7, 4, 6)
(10, 5, 2, 6, 3, 8)

So lets say I want to return the first time step that the number 10 appears. Knowing that that is time step 5 and 10 is the last in the row I could hard code this:

for i in zip(t,a,b,c,d,e):
    if i[5] == 10:
        print(i[0])
5

I tried to solve the problem this way but I cant quite work out where I am going wrong, any pointers would be welcome.

for i in zip(t,a,b,c,d,e):
    if i[1:] == 10:
        print(i[0])
ojp
  • 973
  • 1
  • 11
  • 26
  • Does this answer your question? [How to check if a tuple contains an element in Python?](https://stackoverflow.com/questions/17920147/how-to-check-if-a-tuple-contains-an-element-in-python) – Pranav Hosangadi Jul 22 '20 at 18:44
  • 2
    `if 10 in i:print(i[0]);break` – Sayandip Dutta Jul 22 '20 at 18:45
  • Thanks pranav, not quite. The order is important which is why I am using a loop. Ie 10 could appear further down row wise but I only care about the first instance of it since this is actually a time series – ojp Jul 22 '20 at 18:49

5 Answers5

1

You can use this approach

 from random import randint
    
    t = list(range(1,11))
    a = [randint(1, 10) for i in range(0, 10)]
    b = [randint(1, 10) for i in range(0, 10)]
    c = [randint(1, 10) for i in range(0, 10)]
    d = [randint(1, 10) for i in range(0, 10)]
    e = [randint(1, 10) for i in range(0, 10)]
    
    for i in zip(t,a,b,c,d,e):
        print (i)
    
    for lst in zip(t,a,b,c,d,e):
        if 10 in lst:
            print(lst[0])
            break
 

  
klv0000
  • 174
  • 10
1

replace

for i in zip(t,a,b,c,d,e):
    if i[1:] == 10:
        print(i[0])

with

for i in zip(t,a,b,c,d,e):
    if 10 in i:
        print(i[0])
        break
nishant
  • 2,526
  • 1
  • 13
  • 19
1

I suggest using random.seed(), to make examples reproducible (tutorial).

From there, as you did:

from random import randint
import random

random.seed(42)
ten_numbers = range(0, 10)
t = list(range(1,11))
a = [randint(1, 10) for i in ten_numbers]
b = [randint(1, 10) for i in ten_numbers]
c = [randint(1, 10) for i in ten_numbers]
d = [randint(1, 10) for i in ten_numbers]
e = [randint(1, 10) for i in ten_numbers]

Can continue with:

for index, each in enumerate(zip(t,a,b,c,d,e)):
    if any([EACH == 10 for EACH in each]):
        print(index)
        break
else:
    print("10 couldn't be found")

Where we used a for-else clause.

theX
  • 1,008
  • 7
  • 22
zabop
  • 6,750
  • 3
  • 39
  • 84
1

Just like everyone else, add a break statement in your if statement. That'll get you out of the loop.

for i in zip(t, a, b, c, d, e):
    if 10 in i[1:]:
        print(i[0])
        break

But, as a bonus, you can add an else statement at the bottom of your for loop if you want to know if 10 was never in i[1:] like so:

for i in zip(t, a, b, c, d, e):
    if 10 in i[1:]:
        print(i[0])
        break
else:  # If the loop never exited abnormally
    print("10 was never in i[1:]")

To learn more about the for/else syntax, check out this StackOverflow post.

theX
  • 1,008
  • 7
  • 22
0

You can use the break call in order to stop a loop once a condition is met.

list = [1,2,3,4,5,6,7,8,9,10]
target = 5
time_step = 0

for time_step, value in enumerate(list):
    if value == target:
        break

If you are dealing with multiple lists, I would suggest checking which list has the value you are searching for, and then extracting its index.

lists = [[1,2,3],
         [4,5,6],
         [7,8,9]]

target = 5 
time_step = 0

for list in lists:
    if target in list:
        for time_step, value in enumerate(list):
            if value == target:
                break
Benjamin Kolber
  • 146
  • 1
  • 8