利用循环蟒蛇MAX()函数 [英] Max() function using python from loop

查看:316
本文介绍了利用循环蟒蛇MAX()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个程序,以确定从声音样本的最大值。循环返回所有样本的数值,但我无法弄清楚如何打印最大的。

  DEF最大的():
F = pickAFile()
听起来= makeSound(F)
因为我在范围内(1,的getLength(音)):
  值= getSampleValueAt(音,I)
打印MAX([值])


解决方案

尝试:

  DEF最大的():
    F = pickAFile()
    听起来= makeSound(F)
    值= []
    因为我在范围内(1,的getLength(音)):
      value.append(getSampleValueAt(音,I))
    打印MAX(值)

或者

  DEF最大的():
    F = pickAFile()
    听起来= makeSound(F)
    打印MAX(getSampleValueAt(声音,我)对我的range(1,的getLength(音)))

通过您的code,在每次迭代覆盖。如果你把所有的值的列表,你可以使用最大然后找到最大值。

另请参见:

I am trying to write a program to determine the maximum value of a sample from a sound. The loop returns the values of all the samples, however I cannot figure out how to print the largest.

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

解决方案

Try:

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

Or

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

With your code, value is overwritten at each iteration. If you make a list with all the values, you can then find the max using max.

Also see:

这篇关于利用循环蟒蛇MAX()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆