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
.
这篇关于在嵌套列表中查找最小值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在嵌套列表中查找最小值?
- 计算测试数量的Python单元测试 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01