2017-04-24 76 views
0

我有numpy字符串数组,我想根据引用字符串测试并根据字符串数组的每个元素是否包含引用字符串来输出布尔数组。我有一个解决方案,但想要更优雅/高效的,可能在纯python实施。感谢您的任何意见。测试字符串数组的每个元素并输出布尔数组

import numpy as np 
import pandas as pd 
import re 
myarray = np.array(['abc1', 'abc2', 'abc3']) 
refstring = 'c2' 
pd.Series(myarray).apply(lambda x: re.search(refstring, x)).astype('bool') 

    >> 0 False 
    >> 1  True 
    >> 2 False 
    >> dtype: bool 
+0

'地图(拉姆达X:refstring在X,[ 'ABC1', 'ABC2' ...])' –

+1

感谢您的平原和简单的解决方案@RobertSeaman – koch

+0

不客气。请接受我的答案,如果这为你工作。 –

回答

1

按我的评论:

map(lambda x: refstring in x, ['abc1', 'abc2'...])