在嵌套列表中查找最小值?

Finding lowest value within a nested list?(在嵌套列表中查找最小值?)

本文介绍了在嵌套列表中查找最小值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个函数,该函数接受一个列表并可以打印该列表中的最小整数.现在我试图弄清楚如何处理嵌套列表,如果最低数字在这些嵌套列表之一内,那么总体上它将打印该数字.我的代码在这里:

def listMin():list2 = [3,4,[2,99,8],7]对于范围内的 i (len(list2)):if type(list2[i]) == type([]):y=min(i)list2.append(y)打印你好"如果 len(list2)== 0:返回无别的:x= 分钟(列表 2)打印 x列表最小值()

虽然这看起来应该打印数字 2,但它不会,并且一旦到达嵌套列表就会给我一个错误说:

TypeError: 'int' 对象不可迭代

我尝试了多种方法,但我很难弄清楚为什么这种方法不起作用.

解决方案

Nesting One Deep

在您的示例中,列表仅嵌套了一层.如果一般都是这种情况,请尝试:

<预><代码>>>>list2 = [3,4,[2,99,8],7]>>>min(x if isinstance(x, int) else min(x) for x in list2)2

任意深度嵌套

如果允许更深的嵌套,定义这个递归函数:

<预><代码>>>>def rmin(lst): return min(x if isinstance(x, int) else rmin(x) for x in lst)...

运行中:

<预><代码>>>>最小(列表2)2

或者,使用更深的嵌套:

<预><代码>>>>list3 = [3,4,[[2,99],8],7]>>>rmin(list3)2>>>list4 = [3, 4, [[2, [99, 1]], 8], 7]>>>rmin(list4)1

工作原理

函数 rmin 由一行组成:

return min(x if isinstance(x, int) else rmin(x) for x in lst)

如您所见,这是一个列表推导式,它查看列表 lst 的每个值 x.

让我们将 min 的参数分成两部分.第一个是:

x if isinstance(x, int) else rmin(x)

如果 x 是整数,则返回 x.否则,它会在 x 上调用 rmin.在后一种情况下,rmin 递归查看 x 中的每个值并返回最小值.

min 参数的第二部分是:

for x in lst

这只是列表理解的常用方法.它依次提取lst中的每个值并将其分配给x.

Im trying to write a function that takes a list and can print the lowest integer that is within that list. Now i'm trying to figure out what to do where this works with nested lists that if the lowest number is within one of those nested lists then overall it will print that number. My code is here:

def listMin():
list2 = [3,4,[2,99,8],7]

for i in range (len(list2)):
    if type(list2[i]) == type([]):



        y=min(i)
        list2.append(y)
        print "hello"
    if len(list2)== 0:
        return None
    else:


        x= min(list2)
        print x


listMin()

while this seems like it should print the number 2 it doesnt and just gives me an error once it reaches the nested list saying:

TypeError: 'int' object is not iterable

ive tried multiple things but i'm having a hard time as to why this sort of thing isn't working.

解决方案

Nesting One Deep

In your example, the list is nested only one deep. If this is the case in general, then try:

>>> list2 = [3,4,[2,99,8],7]
>>> min(x if isinstance(x, int) else min(x) for x in list2)
2

Nesting of Arbitrary Depth

If deeper nesting is allowed, define this recursive function:

>>> def rmin(lst): return min(x if isinstance(x, int) else rmin(x) for x in lst)
... 

In operation:

>>> rmin(list2)
2

Or, with deeper nesting:

>>> list3 = [3,4,[[2,99],8],7]
>>> rmin(list3)
2
>>> list4 = [3, 4, [[2, [99, 1]], 8], 7]
>>> rmin(list4)
1

How it works

The function rmin consists of the single line:

return min(x if isinstance(x, int) else rmin(x) for x in lst)

As you can see, this is a list comprehension that looks at every value x of the list lst.

Let's divide the argument of min into two parts. The first is:

x if isinstance(x, int) else rmin(x)

This returns x if x is an integer. Otherwise, it calls rmin on x. In the latter case, rmin recursively looks at every value in x and returns the minimum.

The second part of the argument of min is:

for x in lst

This is just the usual for a list comprehension. It extracts each value in lst in turn and assigns it to x.

这篇关于在嵌套列表中查找最小值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在嵌套列表中查找最小值?

基础教程推荐