如何仅从字符串中选择字符?

时间:2018-07-30 20:34:59

标签: python-3.x

这是我的数组:

array([['Family one206(+33%)1,164(+179%)0418'],
       ['Family one206(+33%)1,164(+179%)0418'],
       ['Family Two(-67%)23(-47%)23'],
       ['Family Two(+56%)322(-0.6%)1080'],
       ['Family Two(+56%)322(-0.6%)1080'],
       ['Family Two(+56%)322(-0.6%)1080'],
       ['Family Two(+56%)322(-0.6%)1080'],
       ['Family Three5,250(+2.6%)36,410(-2.7%)260412'],
       ['Family Three5,250(+2.6%)36,410(-2.7%)260412'],
       ['Family Three5,250(+2.6%)36,410(-2.7%)260412'],
       ['Family Three5,250(+2.6%)36,410(-2.7%)260412'],
       ['Family Three5,250(+2.6%)36,410(-2.7%)260412'],
       ['Family Three5,250(+2.6%)36,410(-2.7%)260412'])

但是我只希望我的数组看起来像:

array([['Family one'],
       ['Family one'],
       ['Family Two'],
       ['Family Two'],
       ['Family Two'],
       ['Family Two'],
       ['Family Two'],
       ['Family Three'],
       ['Family Three'],
       ['Family Three'],
       ['Family Three'],
       ['Family Three'],
       ['Family Three']])

我不确定如何只能从混合字符串中选择字符串。

2 个答案:

答案 0 :(得分:1)

您可以使用正则表达式:

[{
  "data":{  
    "info1":"something here"
 }
}]

答案 1 :(得分:1)

您可以决定用无空格替换从非单词开始的位置到下一个也是非单词的所有内容 假设a为上方的数组,我们可以这样做:

import re
s =  np.array([[re.sub(r"(?i)[^A-Z](?![A-Z]).*","",i)] for i in a.squeeze()])
s
array([['Family one'],
       ['Family one'],
       ['Family Two'],
       ['Family Two'],
       ['Family Two'],
       ['Family Two'],
       ['Family Two'],
       ['Family Three'],
       ['Family Three'],
       ['Family Three'],
       ['Family Three'],
       ['Family Three'],
       ['Family Three']], dtype='<U12')