Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在文本文件的列中查找本地最大值_Python_Max - Fatal编程技术网

Python 在文本文件的列中查找本地最大值

Python 在文本文件的列中查找本地最大值,python,max,Python,Max,我有一个包含两行的文本文件。我想找到第一排的局部最大值。我正在尝试下面的代码,但我不知道为什么会出现此错误:“x必须是1D数组” 输入如下: 0 5 1 5 2 5 3 6 1 6 0 7 0 6 0.01 5 0.4 5 0.001 5 0.3 6 0.7 6 1.5 7 4 6 2 5 0.1 6 0 6 输出应为: 3 6 0.4 5 4 6 a应该是一个列表,但是,您不是在制作列表 f = open ('ttt.txt', 'r') data = f.readlines() a =

我有一个包含两行的文本文件。我想找到第一排的局部最大值。我正在尝试下面的代码,但我不知道为什么会出现此错误:“
x
必须是1D数组”

输入如下:

0 5 
1 5
2 5
3 6
1 6
0 7
0 6
0.01 5
0.4 5
0.001 5
0.3 6
0.7 6
1.5 7
4 6
2 5
0.1 6
0 6
输出应为:

3 6
0.4 5
4 6

a
应该是一个列表,但是,您不是在制作列表

f = open ('ttt.txt', 'r')
data = f.readlines()
a = [float(line.split()[0]) for line in data]
a = np.array(a)
peaks, _ = find_peaks(a, height=0)
您可以使用from
scipy.signal
。它返回数组中的索引

此外,要从文件加载数据,可以使用。它以
numpy
数组返回数据(随时可用)

代码如下:

# Import modules
import numpy as np 
from scipy.signal import argrelextrema

# Load the data from text file
data = np.loadtxt('ttt.txt')

# Get local maxima from the first column
index = argrelextrema(data[:, 0], np.greater)

print(index)
# (array([ 3,  8, 13], dtype=int64),)
print(data[index])
# [[3.  6. ]
#  [0.4 5. ]
#  [4.  6. ]]

请看一看。然后通过分享你的数据样本来编辑问题。你是指第一列的局部最大值吗?“7”来自哪里?在您的代码段中没有名为
x
。请发布一个适当的最小可复制示例。错误中有“x”。这是python代码计算的一部分!!这是有效的。非常感谢。但是我怎么知道“a”应该是一个列表,因为我有一个错误,就是“x必须是1D数组”。我猜
x
find\u peaks
:)中的变量名
# Import modules
import numpy as np 
from scipy.signal import argrelextrema

# Load the data from text file
data = np.loadtxt('ttt.txt')

# Get local maxima from the first column
index = argrelextrema(data[:, 0], np.greater)

print(index)
# (array([ 3,  8, 13], dtype=int64),)
print(data[index])
# [[3.  6. ]
#  [0.4 5. ]
#  [4.  6. ]]