在列表中查找最小值

时间:2019-06-28 17:59:40

标签: python list minimum

我想在Python的列表列表中找到最小值。示例:

If

' Get all the `td` tags Dim tableDataNodes As Variant Set tableDataNodes = Ie.Document.getElementsByTagName("td") Dim dataNode As Variant For Each dataNode In tableDataNodes ' See if innerText of `td` is a value you are looking for. Dim fruitValue As String Select Case dataNode.innerText Case "Apple" ' Get the text of the next sibiling fruitValue = dataNode.nextElementSibling.innerText ' Add value to range or whatever you want to do with it. ThisWorkbook.worksheets("Sheet1").range("A1").value = fruitValue Case "Orange" ' Get the text of the next sibiling fruitValue = dataNode.nextElementSibling.innerText ' Add value to range or whatever you want to do with it. ThisWorkbook.worksheets("Sheet1").range("A2").value = fruitValue End Select Next dataNode 中的最小值为ls = [[2,3,5],[8,1,10]] 。我如何以一种简单的方式获得它?

3 个答案:

答案 0 :(得分:4)

# find the min of each sublist, then find the global min
min(map(min, ls))
# 1

# flatten `ls`, then find the min
min(y for x in ls for y in x)
# 1

# use itertools.chain to flatten `ls`
min(itertools.chain.from_iterable(ls))
# 1

ls = [[2, 3, 5], [8, 1, 10]] * 10000

%timeit min(y for x in ls for y in x)            # 7.31 ms ± 64.9 µs
%timeit min([min(l) for l in ls])                # 6.24 ms ± 56.9 µs
%timeit min(map(min, ls))                        # 5.44 ms ± 151 µs
%timeit min(min(ls, key=min))                    # 5.28 ms ± 129 µs
%timeit min(itertools.chain.from_iterable(ls))   # 2.67 ms ± 62.5 µs

答案 1 :(得分:1)

min_abs = min([min(l) for l in ls])

答案 2 :(得分:1)

您可以尝试以下操作:

res = min(min(ls, key=min))