我有两个列表,我试图分开,所以我可以计算最小值和最大值。原始列表看起来像这样:
vlim: [-6.44194488 -4.12189618]
vlim: [-0.35109226 0.90269746]
vlim: [-6.48470812 -4.06394711]
vlim: [-0.35046298 0.89800871]
vlim: [-6.50338849 -3.92795199]
以下你可以看到我的剧本:
'''
Finding max and min vlim values
stars is lists[0] and gas is lists[1]
'''
from sys import argv
script, file = argv
with open(file) as f:
lists = ([], [])
for i, line in enumerate(f):
lists[i%2].append(line.strip())
## stars ##
minlist = []
maxlist= []
for i in range(0,len(lists[0])):
min_tmp = lists[0][i][7:18]
max_tmp = lists[0][i][19:30]
minlist.append(min_tmp)
maxlist.append(max_tmp)
## gas ##
minlist2 = []
maxlist2 = []
for i in range(0,len(lists[1])):
min_tmp2 = lists[1][i][7:18]
max_tmp2 = lists[1][i][19:30]
minlist2.append(min_tmp2)
maxlist2.append(max_tmp2)
print 'min star: ', max(minlist) #max bc negative values
print 'max star: ', min(maxlist)
print 'min gas: ', max(minlist2)
print 'max gas: ', max(maxlist2)
print''
print minlist
print''
print minlist2
print ''
print maxlist
我想摆脱" vlim"和括号,所以我做了两个新的名单; " minlist"它抓住了第一个数字和#34; maxlist"抓住了第二个。但是,当我在最大值的气体列表上运行max()时,它不会选择实际的最大值:
min star: -7.48815421
max star: -3.84855181
min gas: -0.97899559
max gas: 0.9262432]
但是你可以在下面的列表中看到,有一些数字大于max()找到的数字,见第2行:
[' 0.90269746', ' 0.89800871', ' 0.91088956', ' 0.92375575', ' 0.91859109', ' 0.91030079', ' 0.91962641', '0.9262432]', ' 0.93031583', ' 0.96124881', \
' 0.97476199', ' 0.96566733', ' 0.96988751', ' 0.98140795', ' 0.96833185', ' 0.96395218', ' 0.9342977 ', ' 0.93163674', ' 0.89933621', ' 0.91770517', \
' 0.92165692', ' 0.90245033', ' 0.89833043', ' 0.91639502', ' 0.88873233', ' 0.87241003', ' 0.78407889', ' 0.69231273', ' 0.63886276', ' 0.60207066', \
' 0.58330105', ' 0.56990499', ' 0.55740021', ' 0.53945954', ' 0.51366063', ' 0.51928393', ' 0.46654584', ' 0.43496984', ' 0.39934928', ' 0.36126022', \
' 0.32730902', ' 0.30790094', ' 0.28456324', ' 0.25893116', ' 0.24498652', ' 0.23020501', ' 0.20266526', ' 0.18111511', ' 0.16598142', ' 0.18298263', \
' 0.15324247', ' 0.14616999', ' 0.11671082', ' 0.08299617', ' 0.04595069', ' 0.00824732', '-0.03321626', '-0.06159366', '-0.09449168', '-0.11242293', \
'-0.09726435', '-0.06395773', '-0.04232095', '-0.03196949', '-0.03847378', '-0.05006481', '-0.05253942', '-0.08344223', '-0.10788819', '-0.12003492', \
'-0.14036405', '-0.16807319', '-0.18165499', '-0.17822416', '0.1829591]', '-0.18755693', '-0.20039758', '-0.20103783']
知道如何解决这个问题吗?肯定会发生一些奇怪的事情,因为我还必须使用max()on" minlist2" (并且最小化" maxlist")让它给我列表中最负数(或最小负数)。它与正数和负数都有关吗?
我对python真的很新,所以我可能只是在我的脚本中犯了一些错误而没有意识到它......
对于这篇文章感到抱歉。谢谢!
答案 0 :(得分:-1)
你在做什么就像是:
a = ['1','3','10','5']
print sorted(a)
输出
['1', '10', '3', '5']
因此,第一步是转换为float / int
a = ['1','3','10','5']
print sorted(a)
print sorted(map(int,a))
print max(a)
print max(map(int,a))
输出
['1', '10', '3', '5']
[1, 3, 5, 10]
5
10