ValueError: attempt to get argmax of an empty sequence

When you run the code below, the following error appears.

ValueError: attempt to get argmax of an empty sequence

The code processes information from images sent to it from the simulator.

It works fine at first, but when the array is Rover.nav_anglesempty, I get an error, although there is an if condition

if Rover.nav_angles is not None:
        Max_angle_points=np.argmax(Rover.nav_angles)
        MAX_DIST=np.max(Rover.nav_dists[Max_angle_points])
+4
source share
2 answers

Using:

if Rover.nav_angles:
    ...

Check emptiness and None. But it seems that you are dealing with an array numpy, so use:

if Rover.nav_angles.size:
    ...
0
source

In python, it is best tryto do things rather than set conditions around it.

try: 
    Max_angle_points=np.argmax(Rover.nav_angles)
    MAX_DIST=np.max(Rover.nav_dists[Max_angle_points])
except ValueError:
    pass
   # specify what the code should do, if the exception occurs.

To your real problem: emptydoes not necessarily mean your array None. If you want to check with the condition, try

if Rover.nav_angles:
    Max_angle_points=np.argmax(Rover.nav_angles)
    MAX_DIST=np.max(Rover.nav_dists[Max_angle_points])
0
source

Source: https://habr.com/ru/post/1686133/


All Articles