从阵列中取出一个范围

时间:2016-07-20 13:03:08

标签: python arrays numpy range

我有一个名为wave_data的不同数字的数组。它有从1030000001到0.60000002的101个数字。

这是我的代码:

center_wave = 450e-9
width = 50e-9
wavelengths = wave_data*1e-6

range = width/2
min = center_wave - range
max = center_wave + range

wavelengths = wavelengths[somevariable:somevariable]

目标是让这两个数字(最小和最大变量)成为从数组中选择数字的范围。但是,我在这一点上陷入困​​境,不知道该怎么做。

2 个答案:

答案 0 :(得分:4)

Select by boolean mask,而不是切片:

document.getElementById('liveLeft') # Will return null as iframe has seperate DOM

var doc = document.getElementById('win_old').contentDocument # Loads iframe DOM elements in the variable doc
doc.getElementById('liveLeft') # Will now return the desired element.

提示:不要将变量命名为waverange = width/2 wavemin = center_wave - waverange wavemax = center_wave + waverange mask = (wavelengths > wavemin) & (wavelengths <= wavemax) wavelengths = wavelengths[mask] rangemin,因为这会影响Python内置的相同名称。

答案 1 :(得分:1)

可以通过简单的列表理解来完成。

center_wave = 450e-9
width = 50e-9
wavelengths = wave_data*1e-6

wave_range = width/2
wave_min = center_wave - wave_range
wave_max = center_wave + wave_range

wavelengths = [x for x in wavelengths if x >= wave_min and x <= wave_max]