Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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 查找max并从列表中提取数据_Python_Python 3.x - Fatal编程技术网

Python 查找max并从列表中提取数据

Python 查找max并从列表中提取数据,python,python-3.x,Python,Python 3.x,我有一个文本文件,有20个汽车价格和它的序列号。这个文件有50行。我想找到每10行的最高汽车价格及其序列号 priceandserial.txt 102030 4000.30 102040 5000.40 102080 5500.40 102130 4000.30 102140 5000.50 102180 6000.50 102230 2000.60 102240 4000.30 102280 6000.30 102330 9000.70 102340 1000.30 102380 3000.

我有一个文本文件,有20个汽车价格和它的序列号。这个文件有50行。我想找到每10行的最高汽车价格及其序列号

priceandserial.txt

102030 4000.30
102040 5000.40
102080 5500.40
102130 4000.30
102140 5000.50
102180 6000.50
102230 2000.60
102240 4000.30
102280 6000.30
102330 9000.70
102340 1000.30
102380 3000.30
102430 4000.80
102440 5000.30
102480 7000.30
当我尝试Python的内置max函数时,得到的最大值是102480

x = np.loadtxt('carserial.txt', unpack=True)

print('Max:', np.max(x))  

预期结果:

102330 9000.70
102480 7000.30

文件中有50行,因此我应该有一个5行的结果,每10行的序列和最大价格。

您应该迭代它,并为每10行提取最大价格:

import math

# New empty list for colecting the results
max_list=[]

#iterate thorught x supposing
for i in range(math.ceil(len(x)/10)):
   ### append only 10 elments if i+10 is not superior to the lenght of the array
   if i+11<len(x):    
       max_list=max_list.append(np.max(x[i:i+11]))
   ### if it is superior, then append all the remaining elements
   else:
       max_list=max_list.append(np.max(x[i:]))


您可以先转置输入,然后使用np.split,并为每个子矩阵计算其最大值

x = np.genfromtxt('carserial.txt', unpack=True).T
print(x)
for submatrix in np.split(x,len(x)//10):
    print(max(submatrix,key=lambda l:l[1]))

恕我直言,我认为第一个解决方案设计过度。这项任务不需要numpy或math,只需要一本字典。在循环过程中,如果最新值大于当前值,则更新字典,如果不大于当前值,则不执行任何操作。在第10项中,将字典中的值附加到输出列表并重置缓冲区

with open('filename.txt', 'r') as opened_file:
    data = opened_file.read()

rowsplitdata = data.split('\n')
colsplitdata = [u.split(' ') for u in rowsplitdata]
x = [[int(j[0]), float(j[1])] for j in colsplitdata]

output = []
buffer = {"max":0, "index":0}
count = 0
#this assumes x is a list of lists, not a numpy array
for u in x:
    count += 1
    if u[1] > buffer["max"]:
        buffer["max"] = u[1]
        buffer["index"] = u[0]
    if count == 10:
        output.append([buffer["index"], buffer["max"]])
        buffer = {"max":0, "index":0}
        count = 0
#append the remainder of the buffer in case you didn't get to ten in the final pass
output.append([buffer["index"], buffer["max"]])
output
[[102330, 9000.7], [102480, 7000.3]]

这应该是你的工作

number_list = [[],[]]
with open('filename.txt', 'r') as opened_file:
    for line in opened_file:
        if len(line.split()) == 0:
            continue
        else:
            a , b = line.split(" ")
            number_list[0].append(a)
            number_list[1].append(b)
col1_max, col2_max = max(number_list[0]), max(number_list[1])
col1_max, col2_max

只需更改文件名。col1_max、col2_max具有各自列的最大值。您可以编辑代码以容纳更多列

请注意,您使用的不是Python的内置max函数,而是Numpy的max函数。你得到的最大值是102480,这并不奇怪,因为你没有告诉numpy你感兴趣的是哪一列,所以它只看第一列。您没有告诉它查看10行数据块中的数据,所以它查看所有数据块-所有行的第一列的最大值为102480。也许你可以试着解决这个问题,问一下这个解决方案有什么问题?是的,我很抱歉。我是一个初学者,我唯一的资源是在线资料。我最近听说stackoverflow,我可以在那里提问。我之所以使用numpy,是因为我跟踪了一段youtube视频,那个人总是使用import numpy作为np。我不知道它是做什么的,但我已经做到了。得到这个错误:如果u[1]>buffer[max]:TypeError:“>”在'str'和'int'的实例之间不受支持,你需要将solumns解析成float,我假设你已经这样做了。正在更新代码以反映这一点。仍在获取错误:第36行,在x=[[intj[0]中,colsplitdata]文件C:\lara\loop.py中,第36行,在x=[[intj[0],colsplitdata中,j的floatj[1]]中,colsplitdata中,j的floatj[1]]值错误:以10为基数的int无效文本:在colsplitdata中,尝试[[j[0],floatj[1]]。如果这引发错误,则源数据文件与所述不符。您也可以尝试在拆分时用\r\n替换