我有一个文本文件与二十个汽车价格和它的序列号有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行,因此我应该有一个序列和每10行最大价格5行的结果。你知道吗
恕我直言,我认为第一个解决方案设计过度了。这个任务不需要
numpy
或math
,只需要一本字典。在循环过程中,如果最新值大于当前值,则更新字典;如果不大于当前值,则不执行任何操作。在第10项中,将字典中的值附加到输出列表并重置缓冲区。你知道吗您应该对其进行迭代,并为每10行提取最大值:
这应该是你的工作。你知道吗
只需更改
filename
。col1_max
,col2_max
具有相应列的最大值。您可以编辑代码以容纳更多列。你知道吗相关问题 更多 >
编程相关推荐