[英]Use “Findall” operation on Python array of strings
我有一个 1m+ 行的数据集,每一行都有小写/大写字母、符号和数字的组合。 我希望清理这些数据,只保留小写字母和数字并排的最后一个实例。 为了提高速度效率,我目前的计划是将这些数据作为一个字符串数组,然后使用 .findall 操作来保留我正在寻找的字母/数字组合。
这是我正在尝试做的事情:
输入
list = Array(["Nd4","0-0","Nxe4","e8+","e4g2"])
newList = list.findall('[a-z]\d')[len(list.findall('[a-z]\d')-1]
newList 的预期输出
newList = ("d4","","e4","e8","g2")
不建议使用“list”来分配变量,因为它是一个内置函数
import re
import numpy as np
lists = np.array(["Nd4","0-0","Nxe4","e8+","e4g2"])
def findall(i,pattern=r'[a-z1-9]+'):
return re.findall(pattern,i)[0] if re.findall(pattern,i) else ""
newList = [findall(i) for i in lists]
# OR if you want to return an array
newList = np.array(list(map(findall,lists)))
# >>> ['d4', '', 'xe4', 'e8', 'e4g2']
这可能不是最漂亮的方式,但我认为它可以完成工作!
import re
import numpy as np
lists = np.array(["Nd4","0-0","Nxe4","e8+","e4g2"])
def function(i):
try:
return re.findall(r'[a-z]\d',i)[len(re.findall(r'[a-z]\d',i))-1]
except:
return ""
newList = [function(i) for i in lists]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.