Value error, truth error, ambiguous error

不问归期 提交于 2019-12-13 00:28:40

问题


When using this code

 for i in range(len(data)):
   if Ycoord >= Y_west and Xcoord == X_west:
        flag = 4

I get this ValueError

if Ycoord >= Y_west and Xcoord == X_west: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

then i use the above restriction

Any help on how i can keep my restriction and go on with the writing of my file?


回答1:


The variables Ycoord and Xcoord are probably numpy.ndarray objects. You have to use the array compatible and operator to check all its values for your condition. You can create a flag array and set the values to 4 in all places where your conditional is True:

check = np.logical_and(Ycoord >= Y_west, Xcoord == X_west)
flag = np.zeros_like(Ycoord)
flag[check] = 4

or you have to test value-by-value in your code doing:

for i in range(len(data)):
    if Ycoord[i] >= Y_west and Xcoord[i] == X_west:
        flag = 4


来源:https://stackoverflow.com/questions/22482003/value-error-truth-error-ambiguous-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!