Search code examples
pythonmathexp

How to find largest number from a set of exponential numbers in python


I have a list of lists with exponential numbers as follows:

[[  1.92043482e-04   0.00000000e+00   0.00000000e+00   0.00000000e+00
    0.00000000e+00   0.00000000e+00   2.41005634e-03   0.00000000e+00
    7.19330120e-04   0.00000000e+00   0.00000000e+00   1.42886875e-04
    0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
    0.00000000e+00   9.79279411e-05   7.88888657e-04   0.00000000e+00
    0.00000000e+00   1.40425916e-01   0.00000000e+00   1.13955893e-02
    7.36868947e-03   3.67091988e-04   0.00000000e+00   0.00000000e+00
    0.00000000e+00   0.00000000e+00   1.72037105e-03   1.72377961e-03
    0.00000000e+00   0.00000000e+00   1.19532061e-01   0.00000000e+00
    0.00000000e+00   0.00000000e+00   0.00000000e+00   3.37249481e-04
    0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
    0.00000000e+00   0.00000000e+00   1.75111492e-03   0.00000000e+00
    0.00000000e+00   1.12639313e-02]
 [  0.00000000e+00   0.00000000e+00   1.10271735e-04   5.98736562e-04
    6.77961628e-04   7.49569659e-04   0.00000000e+00   0.00000000e+00
    2.91697850e-03   0.00000000e+00   0.00000000e+00   0.00000000e+00
    0.00000000e+00   0.00000000e+00   3.30257021e-04   2.46629275e-04
    0.00000000e+00   1.87586441e-02   6.49103144e-04   0.00000000e+00
    1.19046355e-04   0.00000000e+00   0.00000000e+00   2.69499898e-03
    1.48525386e-02   0.00000000e+00   0.00000000e+00   0.00000000e+00
    0.00000000e+00   0.00000000e+00   0.00000000e+00   1.18803119e-03
    3.93100829e-04   0.00000000e+00   3.76245304e-04   2.79537738e-02
    0.00000000e+00   1.20738457e-03   9.74669064e-06   7.18680093e-04
    1.61546793e-02   3.49360861e-04   0.00000000e+00   0.00000000e+00
    0.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00
    0.00000000e+00   0.00000000e+00]]

How do I find the largest number within each list in python?


Solution

  • Based on the formatting in the question, it looks like you have a numpy array. If that is the case, you'll do best to use builtin numpy operations. In this case, probably arr.max(axis=1). e.g.:

    >>> a = np.arange(4, dtype=float).reshape(2, 2)
    >>> a
    array([[ 0.,  1.],
           [ 2.,  3.]])
    >>> a.max(axis=1)
    array([ 1.,  3.])
    

    This will be faster than using python's max function (iteration can happen directly on the numpy array data rather than having to go through ndarray.__iter__) and as another bonus, the output will still be a numpy array which you can then use for other calculations, etc.