Finding the smallest number in a python list with scientific notation

Question:

I’m trying to find the smallest number in this list that I created from a txt column, but when I run the code it gives me the wrong smallest number.

This is my code:

import csv

with open('ar_blast.txt') as file:
    reader = csv.reader(file,delimiter='t')

    lista = []
    count = 0

    for row in reader:
        count = count +1
        lista.append(row[10])
        print(lista)
        print(min(lista))

This is my output:

['1.59e-32', '4.57e-32', '2.76e-24', '2.17e-23', '4.73e-10', '0.006', '0.009', '0.012', '0.015', '0.040', '0.083', '0.19', '0.22', '0.72', '0.94', '2.4', '2.5', '3.0', '3.4', '4.9', '5.7', '6.2', '6.3', '9.1', '8.98e-18', '8.49e-14', '1.41e-13', '8.44e-12', '1.20e-11', '2.81e-10', '5.80e-04', '7.95e-04', '0.005', '0.005', '0.006', '0.019', '0.064', '0.065', '0.068', '0.073', '0.19', '0.19', '0.24', '0.47', '0.90', '2.0', '0.94', '1.1', '1.6', '1.6', '1.6', '1.7', '2.0', '2.1', '2.2', '3.3', '3.1', '5.2', '8.0', '9.8', '8.98e-18', '8.49e-14', '1.41e-13', '8.44e-12', '1.20e-11', '2.81e-10', '5.80e-04', '7.95e-04', '0.005', '0.005', '0.006', '0.019', '0.064', '0.065', '0.068', '0.073', '0.19', '0.19', '0.24', '0.47', '0.90', '2.0', '0.94', '1.1', '1.6', '1.6', '1.6', '1.7', '2.0', '2.1', '2.2', '3.3', '3.1', '5.2', '8.0', '9.8']   

0.005

What I want to print is the smallest number which clearly is not 0.005

Asked By: Go4ty01

||

Answers:

The problem is that the items in your list are str not numbers. For example:

>>> lista = ["0.005", "1.59e-32"]
>>> min(lista)
'0.005'

To fix that, you can convert the numbers into float like so:

>>> lista = list(map(float, lista))
>>> min(lista)
1.59e-32
Answered By: Anwarvic
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.