如果特定值在以下串列中,我正在嘗試創建一個布爾結果。不幸的是,我正在處理一些我不知道如何解決的錯誤
list = [array(['63977', '63981', '91916', '63908', '97158', '63906', '63910',
'63956', '63970', '63969', 'Other'], dtype='<U5')]
例如,我試圖找到值 63977,而我的嘗試是:
if '63977' in list:
print('find it')
但我收到以下錯誤:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
uj5u.com熱心網友回復:
您已經創建了嵌套串列,因此代碼不會按照您的想法執行。您實際上是在檢查特定元素是否在外部陣列中,但您應該搜索元素是否存在于內部陣列中。
接下來試試:
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')
如果您創建這樣的串列:
my_list = array(['63977', '63981', '91916', '63908', '97158', '63906', '63910',
'63956', '63970', '63969', 'Other'], dtype='<U5')
然后你的代碼就可以作業了:
if '63977' in my_list:
print('find it')
希望有幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/491952.html
上一篇:在熊貓交叉表中使用自定義列串列
