简体   繁体   English

用科学记数法在python列表中找到最小的数字

[英]Finding the smallest number in a python list with scientific notation

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.我试图在这个列表中找到我从 txt 列创建的最小数字,但是当我运行代码时,它给了我错误的最小数字。

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 0.005

What I want to print is the smallest number which clearly is not 0.005我要打印的是最小的数字,显然不是 0.005

The problem is that the items in your list are str not numbers.问题是列表中的str不是数字。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM