Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

min() arg is an empty sequence

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:

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

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(x for x in matr_sum[i] if x > 0)

But with two conditions, it doesn't work.

like image 314
Andrew Avatar asked Nov 24 '14 21:11

Andrew


People also ask

What does Max () arg is an empty sequence mean Python?

The error: “ValueError: max() arg is an empty sequence” occurs when you pass an empty list as an argument to the max() function. The max() function cannot find the largest item in an iterable if there are no items.

What is an empty sequence?

An empty sequence is a (finite) sequence containing no terms. Thus an empty sequence is a mapping from ∅ to S, that is, the empty mapping. Thus by definition an empty sequence is a (finite) sequence whose length is 0.

What is MIN function in Python?

Python min() Function The min() function returns the item with the lowest value, or the item with the lowest value in an iterable. If the values are strings, an alphabetically comparison is done.


1 Answers

If you want to avoid this ValueError in general, you can set a default argument to min(), that will be returned in case of an empty list. See described here.

min([], default="EMPTY")
# returns EMPTY

Note that this only works in Python 3.4+

like image 188
patrick Avatar answered Sep 21 '22 02:09

patrick