简体   繁体   中英

Nested For loop in python which starts second loop at the iterator of the first loop

I need to make the second python loop start following where the iterator of the first loop is at the entry into the second loop

Currently both loops run the entire range

Unfortunately I don't understand this syntax

Full code in the example is here: https://gist.github.com/kylemcdonald/c8e62ef8cb9515d64df4

for i, (start1, stop1) in enumerate(zip(starts, stops)):
    audio1 = y[start1:stop1]

    for j, (start2, stop2) in enumerate(zip(starts, stops)):
        audio2 = y[start2:stop2]

I tried changing to

for i, (start1, stop1) in enumerate(zip(starts, stops)):
    audio1 = y[start1:stop1]

    for j, (start2, stop2) in enumerate(zip(start1, stops)):
        audio2 = y[start2:stop2]

But i get the error: zip argument #1 must support iteration

I also tried the obvious way it would be done in C

for i, (start1, stop1) in enumerate(zip(starts, stops)):
    audio1 = y[start1:stop1]

    for j = i+1, (start2, stop2) in enumerate(zip(start1, stops)):
        audio2 = y[start2:stop2]

But i get the error: SyntaxError: invalid syntax

Im not a python programmer, please help me understand and fix this complex syntax so that loop2 begins at the iterator following the current iterator of loop 1 and goes to the end of the loop

So there are no repeated comparisons and no 2 of the same audio samples are compared to each other

Thanks,

Here's some example code:

a = [1,2,3,4,5]
b = [10,20,30,40,50]

for i, (a1, b1) in enumerate(zip(a, b)):
    for j, (a2, b2) in enumerate(zip(a[i+1:],b[i+1:])):
        print(a1,b1,a2,b2)

Result:

1 10 2 20
1 10 3 30
1 10 4 40
1 10 5 50
2 20 3 30
2 20 4 40
2 20 5 50
3 30 4 40
3 30 5 50
4 40 5 50

This might be what you want:

for i, (start1, stop1) in enumerate(zip(starts, stops)):
    audio1 = y[start1:stop1]

    for start2, stop2 in zip(starts[i+1:], stops[i+1:]):
        audio2 = y[start2:stop2]

which iterates over starts / stops from the current value of i. I removed the second enumerate, as for your question you don't need to produce indexes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM