0

我在这篇文章的底部附上了数据。我有另一个程序的输出,我想以合理的方式在 python 中解析。

输出将始终采用相同的形式,我想做的是找出哪个“标签”具有最大值。我已经标记了下面数据的截断版本(并标记了列的顶部)

0        1               2                3            4        5            6          7              8         
AlII     1.6272514892aa  0.0000042282     5.85216aa    0.68792  10.741103    0.074253   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
AlII     1.6273145922ab  0.0000129136     0.50000ab    1.41279  10.483775    0.516978   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
MgII     1.6272514892AA  0.0000000000     5.85217AA    0.00000  11.510502    3.9484     0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00

所以我想做的是以多种方式解析这个文件。从上面的缩写中,我想返回以下内容:

  • 最大(第 2 列):AlII ab:0.0000129136
  • 最大(第 4 栏):AlII ab:1.41279
  • 最大(第 6 栏):MgII AA:3.9484

而且我实际上想做前 N 个最大值,但对于这个例子,我只会做最大的。我因创建新列表并查找这些新列表的索引而陷入困境。有没有一些直接的方法来做这种解析?或者这将是一个混乱的过程?

编辑

我被要求提供一些代码来显示我尝试过的内容。

import numpy as np
analyseList = []
with open('output.ascii', 'r') as f18:
  for line in f18.read().splitlines():
    analyseList.append(line.split())

for column in [2, 4, 6]:
  winner = analyseList[np.argmax(np.column_stack(np.array(analyseList))[column])]
  print winner[0], winner[1][-2:], winner[column]

当然,这只返回最大值(我希望能够做到 N 最大)。此外,看起来我正在以最简单的方式进行操作。

要获取前 N 个最大值的索引值,我可以这样做(使用来自 的建议):

[list(analyse_array[2]).index(x) for x in heapq.nlargest(5, analyse_array[2])]

我在做一些超级复杂的事情吗?

AlII     1.6272514892aa  0.0000042282     5.85216aa    0.68792  10.741103    0.074253   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
AlII     1.6273145922ab  0.0000129136     0.50000ab    1.41279  10.483775    0.516978   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
AlII     1.6273482473ac  0.0000118688     2.01059ac    3.95256  10.714536    0.646238   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
AlII     1.6273975034ad  0.0000379662     3.78319ad    5.42225  11.118654    0.683413   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
AlII     1.6274362338ae  0.0000137972     2.17566ae    2.17134  11.105118    1.025574   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
AlII     1.6274729985af  0.0000071899     2.59672af    1.62035  11.686851    0.218038   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
AlII     1.6275047836ag  0.0000059328     0.56225ag    0.73486  10.908730    0.729322   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
AlII     1.6275333529ah  0.0000063501     2.46788ah    1.66430  11.280185    0.381467   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
AlII     1.6275811952ai  0.0000273404     4.39490ai    3.90964  11.154901    0.430842   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
AlII     1.6276822766ak  0.0000153451     3.86075ak    1.01659  11.687808    0.302170   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
AlII     1.6277080822al  0.0000041420     2.77773al    0.74546  11.515588    0.594305   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
AlII     1.6277357442am  0.0000180202     3.03343am    2.97410  11.311138    0.474284   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
AlII     1.6277884119an  0.0000100139     3.05039an    1.49107  11.218137    0.271466   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
AlII     1.6278358107cd  0.0000488929     3.80549cd    6.28261  11.103154    1.305702   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
AlII     1.6278657638ao  0.0000095109     4.23025ao    1.26277  11.681221    0.419769   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
AlII     1.6279139560bq  0.0000074039     2.68827bq    1.66201  11.457767    0.630426   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
AlII     1.6280021360cb  0.0000058565     1.55711cb    1.67734  11.188839    0.706256   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
AlII     1.6280408039cc  0.0000168737     3.88995cc    2.74610  11.520547    0.303991   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
AlII     1.6279628448ar  0.0000059612     4.02312ar    2.18053  11.975320    0.228508   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
AlII     1.6280940230as  0.0000033920     0.61137as    0.37590  10.717869    0.253534   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
AlII     1.6281299959at  0.0000035012     0.90119at    0.58387  10.833329    0.072094   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
AlII     1.6281673440au  0.0000048884     0.50000au    0.84155  10.339533    0.191522   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
AlII     1.6282125019av  0.0000068319     7.51956av    1.17482  11.097728    0.069789   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
MgII     1.6272514892AA  0.0000000000     5.85217AA    0.00000  11.510502    0.039484   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgII     1.6273145922AB  0.0000000000     0.50000AB    0.00000  11.176308    0.522596   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgII     1.6273482473AC  0.0000000000     2.01060AC    0.00000  11.450444    0.591724   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgII     1.6273975034AD  0.0000000000     3.78320AD    0.00000  11.803555    0.736543   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgII     1.6274362338AE  0.0000000000     2.17568AE    0.00000  11.967321    0.821801   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgII     1.6274729985AF  0.0000000000     2.59673AF    0.00000  12.458430    0.235348   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgII     1.6275047836AG  0.0000000000     0.56231AG    0.00000  11.941490    0.332862   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgII     1.6275333529AH  0.0000000000     2.46790AH    0.00000  12.037696    0.343867   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgII     1.6275811952AI  0.0000000000     4.39491AI    0.00000  11.840805    0.451418   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgII     1.6276822766AK  0.0000000000     3.86076AK    0.00000  12.497054    0.314167   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgII     1.6277080822AL  0.0000000000     2.77775AL    0.00000  12.391999    0.522446   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgII     1.6277357442AM  0.0000000000     3.03344AM    0.00000  12.112177    0.481882   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgII     1.6277884119AN  0.0000000000     3.05040AN    0.00000  12.013911    0.321297   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgII     1.6278358107CD  0.0000000000     3.80550CD    0.00000  12.061481    1.335432   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgII     1.6278657638AO  0.0000000000     4.23025AO    0.00000  12.520703    0.551397   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgII     1.6279139560BQ  0.0000000000     2.68828BQ    0.00000  12.419351    0.538659   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgII     1.6280021360CB  0.0000000000     1.55713CB    0.00000  11.993443    0.736608   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgII     1.6280408039CC  0.0000000000     3.88996CC    0.00000  12.272450    0.312588   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgII     1.6279628448AR  0.0000000000     4.02313AR    0.00000  12.836074    0.241956   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgII     1.6280940230AS  0.0000000000     0.61143AS    0.00000  11.738664    0.119798   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgII     1.6281299959AT  0.0000000000     0.90123AT    0.00000  11.522748    0.084764   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgII     1.6281673440AU  0.0000000000     0.50000AU    0.00000  11.179284    0.155217   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgII     1.6282125019AV  0.0000000000     7.51956AV    0.00000  11.963228    0.066024   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgII     1.6282950406aw  0.0000162434     0.50000aw   18.49632  10.153640    0.769463   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 1.000E+00 0.000E+00
FeII     1.6276822766AK  0.0000000000     3.86071AK    0.00000  11.593048    0.330634   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
FeII     1.6277080822AL  0.0000000000     2.77768AL    0.00000  11.784789    0.214666   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
FeII     1.6277884119AN  0.0000000000     3.05033AN    0.00000  11.072150    0.276522   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
FeII     1.6278358107CD  0.0000000000     3.80545CD    0.00000  11.147939    1.295144   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
FeII     1.6278657638AO  0.0000000000     4.23021AO    0.00000  11.660933    0.468554   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
FeII     1.6279139560BQ  0.0000000000     2.68821BQ    0.00000  11.427745    0.662669   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
FeII     1.6280021360CB  0.0000000000     1.55700CB    0.00000  10.341299    3.837088   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
FeII     1.6280408039CC  0.0000000000     3.88991CC    0.00000  11.289446    0.290091   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
FeII     1.6279628448AR  0.0000000000     4.02308AR    0.00000  11.989071    0.212467   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
FeII     1.6280940230AS  0.0000000000     0.61111AS    0.00000   9.766184    1.484659   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
FeII     1.6281299959AT  0.0000000000     0.90101AT    0.00000  10.590012    0.186610   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
FeII     1.6281673440AU  0.0000000000     0.50000AU    0.00000  10.186119    0.472566   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
FeII     1.6282125019AV  0.0000000000     7.51954AV    0.00000  11.190375    0.088965   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
AlIII    1.6272514892AA  0.0000000000     5.85216AA    0.00000  10.321950    0.459616   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
AlIII    1.6273145922AB  0.0000000000     0.50000AB    0.00000  10.097437    0.853198   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
AlIII    1.6273482473AC  0.0000000000     2.01059AC    0.00000  10.102194    1.947396   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
AlIII    1.6273975034AD  0.0000000000     3.78319AD    0.00000  11.105979    0.686387   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
AlIII    1.6274362338AE  0.0000000000     2.17566AE    0.00000  11.175948    0.814687   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
AlIII    1.6274729985AF  0.0000000000     2.59672AF    0.00000  11.548086    0.275875   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
AlIII    1.6275047836AG  0.0000000000     0.56225AG    0.00000  11.108123    0.386412   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
AlIII    1.6275333529AH  0.0000000000     2.46788AH    0.00000  11.292689    0.333124   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
AlIII    1.6275811952AI  0.0000000000     4.39490AI    0.00000  11.057781    0.453329   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
AlIII    1.6276822766AK  0.0000000000     3.86075AK    0.00000  11.265981    0.325767   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
AlIII    1.6277080822AL  0.0000000000     2.77773AL    0.00000  11.117329    0.630136   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
AlIII    1.6277357442AM  0.0000000000     3.03343AM    0.00000  10.921103    0.639479   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
AlIII    1.6277884119AN  0.0000000000     3.05039AN    0.00000  11.191087    0.159695   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
AlIII    1.6278657638AO  0.0000000000     4.23025AO    0.00000  11.504725    0.162766   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
AlIII    1.6279139560BQ  0.0000000000     2.68827BQ    0.00000  11.185086    0.780060   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
AlIII    1.6280021360CB  0.0000000000     1.55711CB    0.00000  10.744751    1.475204   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
AlIII    1.6280408039CC  0.0000000000     3.88995CC    0.00000  11.471091    0.278311   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
AlIII    1.6279628448AR  0.0000000000     4.02312AR    0.00000  11.854733    0.209241   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
AlIII    1.6280940230AS  0.0000000000     0.61137AS    0.00000  10.205223    0.810373   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
AlIII    1.6281299959AT  0.0000000000     0.90119AT    0.00000  10.823697    0.155524   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
AlIII    1.6281673440AU  0.0000000000     0.50000AU    0.00000  10.582535    0.258096   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
AlIII    1.6282125019AV  0.0000000000     7.51956AV    0.00000  10.874901    0.182424   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgI      1.6272514892AA  0.0000000000     5.85217AA    0.00000   9.963618    0.199973   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgI      1.6273975034AD  0.0000000000     3.78320AD    0.00000  10.008887    0.626172   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgI      1.6274362338AE  0.0000000000     2.17568AE    0.00000  10.029316    0.772678   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgI      1.6274729985AF  0.0000000000     2.59673AF    0.00000  10.171716    0.390966   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgI      1.6275047836AG  0.0000000000     0.56231AG    0.00000   9.914891    0.378870   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgI      1.6275333529AH  0.0000000000     2.46790AH    0.00000  10.075757    0.224531   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgI      1.6276822766AK  0.0000000000     3.86076AK    0.00000  10.525614    0.306759   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgI      1.6277080822AL  0.0000000000     2.77775AL    0.00000  10.460383    0.403447   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgI      1.6277357442AM  0.0000000000     3.03344AM    0.00000   9.791121    0.536106   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgI      1.6278657638AO  0.0000000000     4.23025AO    0.00000  10.746795    0.126158   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgI      1.6279139560BQ  0.0000000000     2.68828BQ    0.00000  10.134445    0.921959   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgI      1.6280021360CB  0.0000000000     1.55713CB    0.00000   9.964342    0.676227   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgI      1.6280408039CC  0.0000000000     3.88996CC    0.00000  10.126811    0.368050   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgI      1.6279628448AR  0.0000000000     4.02313AR    0.00000  10.750671    0.228670   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgI      1.6280940230AS  0.0000000000     0.61143AS    0.00000   9.405961    0.588696   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgI      1.6281299959AT  0.0000000000     0.90123AT    0.00000   9.292788    0.728340   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
MgI      1.6281673440AU  0.0000000000     0.50000AU    0.00000   9.807055    0.210571   0.0000E+00QA  0.0000E+00  0 [   0.000   0.000 0.000E+00 0.000E+00
4

2 回答 2

1

假设您有一个列表列表开始,您可以只使用 stdlib(未经测试,并且肯定需要调整)来做一些事情,例如:

import heapq
from operator import itemgetter

# get a two tuple of (idx, row)
list_with_index = enumerate(your_list)

# Get the top 10 from column '1' and return a list of those
# which will be (row_index, original_row)
largest = heapq.nlargest(10, list_with_index, key=itemgetter(1, 1))

# Get the top 5 index values
largest_indices = [el[0] for el in largest]
# or map(itemgetter(0), largest) if you prefer...

根据您的示例代码(再次未经测试):

for colno in (2, 4, 6):
    largest = map(itemgetter(0), heapq.nlargest(10, enumerate(analyseList), key=itemgetter(1, colno)))
    print '{} largest: {}'.format(colno, ', '.join(largest))

在旁注中,您还可以考虑查看.argsortnumpy 数组。

于 2012-09-01T11:06:21.010 回答
1

你可以访问 NumPy 包吗?您可以将整个 2D 列表加载到单个列表中ndarray(如果需要,也可以使用第一个字符串列)。然后你可以使用argmax你的数组的方法来找到沿着每一列使用argmax(axis=0)或每一行的最大值的位置argmax(axis=1)......

假设您逐列构造数组,您可以执行以下操作:

analyse_array = np.column_stack(analyse_list)
col_max = analyse_array.argmax(axis=0)

这为您提供了一个数组,该数组给出了每列的最大值位置,作为行的索引。如果您只想要第 2、4、6 列的结果,只需使用花哨的索引:

col_max[[2,4,6]]

这样,您可以将相应的行检索为

analyse_array[col_max[[2,4,6]]]
于 2012-09-01T10:30:04.883 回答