python 怎么取列表中最小的数

Python016

python 怎么取列表中最小的数,第1张

求一个列表中的最小值,可以用min函数。例如:

li = [100,300,200,500,56,23,89]

print("列表中的最小值是:",min(li))

假设有 字典列表 :

要求 price 的 最大值 最小值

最原始的方法:

刚学 Python 时会这么写...

先使用 列表 推导式(list comprehension), 再使用内置函数求 最大值最小值 :

这种方式要遍历 列表 多次.

使用生成器表达式(generator expression)

更简单的写法:

返回整个dict, 不仅仅是price:

总结自: In List of Dicts, find min() value of a common Dict field

源网页: 百度快照

s = input()

#print(s)

b=s.split(',')

#print(b)

for i in range(len(b)):

for j in range(i):

if b[i] <b[j]:

b.insert(j, b.pop(i))

break

print(b[0])