简体   繁体   English

Python 同时循环 output 到列表

[英]Python while Loop output to List

I wrote a simple code that takes as input a.wav file and then finds the frequencies and returns the corresponding note.我编写了一个简单的代码,将 a.wav 文件作为输入,然后找到频率并返回相应的音符。 However, in order to add some more functionality, I need the output to be a list.但是,为了添加更多功能,我需要将 output 作为列表。 A simplified pseudo code of what I have looks like this:我所拥有的简化伪代码如下所示:

while some condition = true:
    compute various things
    print(frequency)

It does print my frequencies correctly, however since it's in a while loop and for every note found it outputs the frequency, the output looks like this:它确实正确地打印了我的频率,但是由于它在一个while循环中并且对于每一个发现它输出频率的音符,output 看起来像这样:

A2
G3
B4
A2
...

When I use list append, it returns me this:当我使用列表 append 时,它返回给我:

["A2"]
["G3"]
["B4"]
["A2"]
...

Whereas I need something like this:而我需要这样的东西:

["A2", "G3", "B4", "A2", ...]

I really don't know how to go about this and to get my output as a list.我真的不知道如何 go 关于这个并将我的 output 作为列表。

Try creating an empty list before the while loop, then appending to it within the loop:尝试在 while 循环之前创建一个空列表,然后在循环中附加到它:

result_list = []
while some condition = true:
    compute various things
    result_list.append(frequency)
    print(frequency)

Have you tried to print the list outside the while block?您是否尝试在 while 块之外打印列表?

result_list = []
while some condition = true:
    compute various things
    result_list.append(frequency)
print(frequency)

I think you should put the print statement outside the while:我认为您应该将 print 语句放在 while 之外:

result_list = []
while some condition = true:
    compute various things
    result_list.append(frequency)
print(frequency)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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