I am trying to create a boolean result if a specific value is inside the following list. Unfortunately i am dealing with some error which i dont know how to fix it
list = [array(['63977', '63981', '91916', '63908', '97158', '63906', '63910',
'63956', '63970', '63969', 'Other'], dtype='<U5')]
For example i was trying to find value 63977 and my try was :
if '63977' in list:
print('find it')
but i got the following error:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
CodePudding user response:
You have created nested list so that code is not doing what you think it is. You are actually checking if specific element is in outer array but instead you should search if element exists in inner array.
Try next:
from numpy import array
my_list = [array(['63977', '63981', '91916', '63908', '97158', '63906', '63910',
'63956', '63970', '63969', 'Other'], dtype='<U5')]
if '63977' in my_list[0]:
print('find it')
If you create list like this:
my_list = array(['63977', '63981', '91916', '63908', '97158', '63906', '63910',
'63956', '63970', '63969', 'Other'], dtype='<U5')
Then your code would work:
if '63977' in my_list:
print('find it')
Hope that helps.