Python min() arg 是一个空序列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27114738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
min() arg is an empty sequence
提问by Andrew
I'm trying to find minimum element in matrix row, but there are two conditions:
1) it must be > 0
2) and this point must be not visited(is_visited[k] is False)
I'm trying to do next:
我试图在矩阵行中找到最小元素,但有两个条件:1) 它必须 > 0 2) 并且必须不访问该点( is_visited[k] is False) 我接下来要尝试:
min(x for x in matr_sum[i] if x > 0 if is_visited[k] is False )
But there is an error: min() arg is an empty sequence
但是有一个错误: min() arg is an empty sequence
The full block of code:
完整的代码块:
for k in range(4):
if matr_sum[i][k] == min(x for x in matr_sum[i] if x > 0 if is_visited[k] is False ) and i!=k:
return k
How to resolve it? Or should I write my min()function? Because it works with one condition:
如何解决?或者我应该写我的min()函数?因为它适用于一个条件:
min(x for x in matr_sum[i] if x > 0)
But with two conditions, it doesn't work.
但是有两个条件,它不起作用。
回答by Kasramvd
Just use andoperation for concatenate tow if statement :
只需使用and操作连接两个 if 语句:
min(x for x in matr_sum[i] if x > 0 and if is_visited[k] is False and i!=k)
回答by Tom Dalton
Try this - it creates the list of x values xsand then only tries to find the min if xsis non-empty. You may need to add some logic to handle the case that xsisempty, depending on what your code is doing.
试试这个 - 它创建 x 值的列表,xs然后只尝试找到xs非空的最小值。您可能需要添加一些逻辑来处理的情况下xs是空的,这取决于你的代码做什么。
for k in range(4):
if is_visited[k] is False and i != k:
xs = [x for x in matr_sum[i] if x > 0]
if xs and matr_sum[i][k] == min(xs):
return k
回答by Steve Jessop
There is no problem with the syntax. It's certainly unusual to have two ifclauses, but it's allowed. Consider:
语法没有问题。有两个if子句当然不寻常,但这是允许的。考虑:
print(min(x for x in range(1,300) if x % 3 == 0 if x % 5 == 0))
Output:
输出:
15
However:
然而:
print(min(x for x in range(1,300) if x % 2 != 0 if x % 2 != 1))
Output:
输出:
ValueError: min() arg is an empty sequence
There are no integers that are both odd and even, so there are no values for minto see, so it throws an exception.
没有既是奇数又是偶数的整数,因此没有min可查看的值,因此它会引发异常。
I deduce that in your code, there are no values that pass both conditions. Python doesn't allow you to compute "the minimum of no values", mainly because it makes no sense.
我推断在您的代码中,没有通过这两个条件的值。Python 不允许您计算“无值的最小值”,主要是因为它没有意义。
You have to decide what you want to do in the case where there is no minimum because there are no values greater than 0. For example, if you don'twant to return kin that case then I might re-write your code something like this:
您必须决定在没有最小值的情况下要做什么,因为没有大于 的值0。例如,如果你不希望返回k在这种情况下,那么我会重新写你的代码是这样的:
for k in range(4):
if k != i and is_visited[k] is False:
if matr_sum[i][k] > 0 and matr_sum[i][k] == min(x for x in matr_sum[i] if x > 0):
return k
It might not be obvious why this helps, but assuming matr_sum[i]is a list or similar, then once we know matr_sum[i][k] > 0then we know the generator passed to minisn't empty, so we won't get an exception. Whereas if matr_sum[i][k] <= 0, then it certainly isn't equal to the smallest positive value, so there's no need to compute the minat all. Another way to write that would be:
为什么这有帮助可能并不明显,但假设matr_sum[i]是一个列表或类似的,那么一旦我们知道matr_sum[i][k] > 0然后我们就知道传递给的生成器min不是空的,所以我们不会得到异常。而如果matr_sum[i][k] <= 0,那么它肯定不等于最小的正值,因此根本不需要计算min。另一种写法是:
if matr_sum[i][k] > 0 and not any(0 < x < matr_sum[i][k] for x in matr_sum[i])
Actually, I'd normally write if not is_visited[k], but I leave it as is Falsesince I don't know whether changing it would change the behaviour of your code.
实际上,我通常会写if not is_visited[k],但我保留它,is False因为我不知道更改它是否会改变您的代码的行为。

