I want to run 2 or 3 For statements at the same time, what should I do?

Asked 2 years ago, Updated 2 years ago, 135 views

for n in [2,0,-2]:

f = int(440*pow(2.,n/12.)+0.5)

freq_of_tones = np.append(freq_of_tones, alone_tone(freq=f,dur=0.3))

for n in [0,2,5]:

j = int(440*pow(2.,n/12.)+0.5)

if n==0:

j=0

freq_of_tones = np.append(freq_of_tones, alone_tone(freq=f,freq1=j,dur=0.3))

pygame_play(freq_of_tones,rate=44100)

It's part of the coding. I'd like to finish the two for statements at the same time, not in order from the top, so please give me a solution. I'm working on a score, and it's the part where I combine the two frequencies to harmonize.

for concurrency

2022-09-21 18:06

1 Answers

Python's zip (*iterables) allows you to tour multiple lists at once.

list1 = [1, 2, 3, 4]
list2 = [100, 120, 30, 300]
list3 = [392, 2, 33, 1]
answer = []
for i, j, k in zip(list1, list2, list3):
   answer.append( i + j + k )

As in the case of the person who asked the question

for n1, n2 in zip([2,0,-2], [0,2,5]):
 f = int(440*pow(2.,n1/12.)+0.5) 
 freq_of_tones = np.append(freq_of_tones,
                           alone_tone(freq=f,dur=0.3))    
 j = int(440*pow(2.,n2/12.)+0.5)
 if n2==0:
 j=0
 freq_of_tones = np.append(freq_of_tones,
                           alone_tone(freq=f,freq1=j,dur=0.3))
pygame_play(freq_of_tones,rate=44100)

It must be the same shape~


2022-09-21 18:06

If you have any answers or tips

Popular Tags
python x 4647
android x 1593
java x 1494
javascript x 1427
c x 927
c++ x 878
ruby-on-rails x 696
php x 692
python3 x 685
html x 656

© 2024 OneMinuteCode. All rights reserved.