Numpy数组正则表达式子 [英] Numpy array Regex sub

查看:439
本文介绍了Numpy数组正则表达式子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定它应该只是一行,但是我无法找出最好的方法:

I am sure it should be just one line but I am not able to figure out the best to do this:

import numpy as np
import re
arr = np.array(["AB", "AC", "XAB", "XAC", "AD"])

我想基于正则表达式匹配"^ A"在开头添加"X".

I want to add "X" in the beginning based on a regex match of "^A".

推荐答案

这是怎么回事:

print(np.array(list(map(lambda v: re.sub(r'^A','XA', v) ,arr))))
% outputs: ['XAB' 'XAC' 'XAB' 'XAC' 'XAD']

这篇关于Numpy数组正则表达式子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆