gpt4 book ai didi

python - 使用Python自循环的Max()函数

转载 作者:行者123 更新时间:2023-12-03 00:06:30 25 4
gpt4 key购买 nike

我正在尝试编写一个程序来确定声音中样本的最大值。该循环返回所有样本的值,但是我不知道如何打印最大的样本。

def largest():
f=pickAFile()
sound=makeSound(f)
for i in range(1,getLength(sound)):
value=getSampleValueAt(sound,i)
print max([value])

最佳答案

尝试:

def largest():
f = pickAFile()
sound = makeSound(f)
value = []
for i in range(1, getLength(sound)):
value.append(getSampleValueAt(sound, i))
print max(value)

要么
def largest():
f = pickAFile()
sound = makeSound(f)
print max(getSampleValueAt(sound, i) for i in range(1, getLength(sound)))

对于您的代码, value在每次迭代时都会被覆盖。如果列出所有值,则可以使用 max找到最大值。

另请参阅:
  • How to find min/max values from rows and columns in Python?
  • https://docs.python.org/2/library/functions.html#max
  • 关于python - 使用Python自循环的Max()函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25550848/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com