给定一个整数列表确实存在一个默认方法找到值之间的最大距离?
所以,如果我有这个数组
[1, 3, 5, 9, 15, 30]
值之间的最大步长为15.列表对象是否有方法执行此操作?
答案 0 :(得分:8)
不,list
个对象没有标准的"相邻差异"方法等。但是,请使用itertools
recipes中提到的pairwise
函数:
def pairwise(iterable):
a, b = tee(iterable)
next(b, None)
return izip(a, b)
...你可以(简明扼要地和)定义
>>> max(b-a for (a,b) in pairwise([1, 3, 5, 9, 15, 30]))
15
答案 1 :(得分:2)
不,但编码很简单:
func recordSound(){
let dirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let recordingName = "my_audio.wav"
let pathArray = [dirPath, recordingName]
let filePath = NSURL.fileURLWithPathComponents(pathArray)
let recordSettings = [AVEncoderAudioQualityKey: AVAudioQuality.Min.rawValue,
AVEncoderBitRateKey: 16,
AVNumberOfChannelsKey: 2,
AVSampleRateKey: 44100.0]
print(filePath)
let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
audioRecorder = try AVAudioRecorder(URL: filePath!, settings: recordSettings as! [String : AnyObject])
} catch _ {
print("Error")
}
audioRecorder.delegate = self
audioRecorder.meteringEnabled = true
audioRecorder.prepareToRecord()
audioRecorder.record()
}
答案 2 :(得分:2)
你可以这样做:
>>> s = [1, 3, 5, 9, 15, 30]
>>> max(x[0] - x[1] for x in zip(s[1:], s))
15
答案 3 :(得分:2)
l=[1, 3, 5, 9, 15, 30]
max([j-i for i, j in zip(l[:-1], l[1:])])
那是使用纯python并为您提供所需的输出“15”。
如果您喜欢使用“numpy”,您可以这样做:
import numpy as np
max(np.diff(l))
答案 4 :(得分:0)
列表对象没有。但是,编写一个能够执行此操作的函数非常快:
def max_step(my_list):
max_step = 0
for ind in xrange(len(my_list)-1):
step = my_list[ind+1] - my_list[ind]
if step > max_step:
max_step = step
return max_step
>>> max_step([1, 3, 5, 9, 15, 30])
15
或者如果你更喜欢更短的时间:
max_step = lambda l: max([l[i+1] - l[i] for i in xrange(len(l)-1)])
>>> max_step([1, 3, 5, 9, 15, 30])
15
答案 5 :(得分:0)
可以使用Me.Invoke(sub()
Dim t As TabPage = New TabPage((k + 1).ToString())
t.Name = k.ToString()
fff(t)
End Sub)
Me.Invoke(sub()
tcViewer.TabPages.Add(t)
End Sub)
函数,但它不是那么优雅,因为你需要一些方法来跟踪以前的值:
reduce()
解决方案的工作原理是将前一个值和累积的最大计算值作为每次迭代的元组进行回归。
[10,20,30,5]的预期结果是什么?是10还是25?如果是25,那么您需要在计算中添加def step(maxStep, cur):
if isinstance(maxStep, int):
maxStep = (abs(maxStep-cur), cur)
return (max(maxStep[0], abs(maxStep[1]-cur)), cur)
l = [1, 3, 5, 9, 15, 30]
print reduce(step, l)[0]
。