【发布时间】:2014-04-29 17:04:14
【问题描述】:
我正在使用 numpy 中的 where 函数在字符串数组中查找单字母字符串。
例如:
我会在['B' 'U' 'A' 'M' 'R' 'O']中寻找'U',得到'U'的索引。
letter = 'U'
row = ['B', 'U', 'A', 'M', 'R', 'O']
letter_found = np.where(row == letter)
但是,当我在寻找一个不存在于字符串数组中的字母时,我得到一个如下所示的空元组:
(array([], dtype=int64),)
我需要能够检测到它何时在数组中找不到我要查找的字母。
我尝试了以下方法:
if not letter_found:
print 'not found'
但这不起作用。如何检测到numpy 中的 where 函数返回的 tuple 为空?是因为我的变量之一可能是错误的类型吗?我是python 的新手,并且是一般的编程人员。
【问题讨论】:
标签: python arrays numpy tuples