python正则表达式不检测方括号(python regex not detecting square brackets)
我有一个场景,我想从给定的内容中除去空格以外的所有特殊字符,我正在使用Python,并且我正在使用这个正则表达式
re.sub(r"[^a-zA-z0-9 ]+","",content)
Itt是删除所有特殊字符,但没有删除方括号
[ ]
,我只是不知道为什么会发生这种情况?之后,我只是使用这个正则表达式
content = re.sub(r"[^a-zA-z0-9 ]+|\[|\]","",content)
它在
IDLE IDE
完美地工作,并删除所有类型的特殊字符,但是当我想替换像维基百科页面这样的大文件时,它现在不会删除方括号]
我只是不知道为什么Python
做这种奇怪的行为,I have a scenario where I want to remove all special characters except spaces from given content and I am working with Python and I was using this regex
re.sub(r"[^a-zA-z0-9 ]+","",content)
Itt was removing all special characters but was not removing square brackets
[ ]
and I just don't know why this happening??after that I just use this regex
content = re.sub(r"[^a-zA-z0-9 ]+|\[|\]","",content)
It's working flawlessly in
IDLE IDE
and removing all kind of special characters but when I want to replace large files like Wikipedia's page then its now not removing closing square brackets]
I just dont whyPython
doing this weird behavior and
原文:https://stackoverflow.com/questions/31225304
最满意答案
假设您沿着
axis
(即0或1)工作。 首先查找1的行数/列数:counts = (arr==1).sum(axis=axis)
您现在可以找到最小计数的索引,从计数列表中选择适当的计数,并将其除以所有其他计数:
counts / counts[counts.argmin()]
请记住最小的数量可以是0。
Assume you work along the axis
axis
(which is 0 or 1). Start by finding the row/column counts of 1's:counts = (arr==1).sum(axis=axis)
You can now find the index of the smallest count, select the appropriate count from the list of counts, and divide all other counts by it:
counts / counts[counts.argmin()]
Just remember that the smallest count can be 0.
相关问答
更多-
我们可以通过为sum函数提供一个axis参数来做到这一点: arr.sum(axis=1) We can simply do this by providing an axis argument to the sum function: arr.sum(axis=1)
-
使用内置type函数获取类型,然后可以使用__module__属性来确定其定义的位置: >>> import numpy as np a = np.array([1, 2, 3]) >>> type(a)
>>> type(a).__module__ 'numpy' >>> type(a).__module__ == np.__name__ True Use the builtin type function to get the type, then you ... -
Python numpy数组根据行为每列分配值(Python numpy array assign values to each column according to row)[2023-07-16]
Python 3中添加了隐式类型转换,因此您的原始代码将按原样运行: from __future__ import division m = 3 n = 10 train = np.empty([n,m], float) for curr_n in range(n): train[curr_n,:] = 1/(curr_n+1) print train 有关__future__模块的一些信息可以在官方文档未来模块中看到 或者,当sshashank124输入他的答案并放入评论时,您可以使用1. ... -
将迭代的值存储到numpy数组 - Python,Numpy(Storing the values of an iteration to a numpy array - Python, Numpy)[2022-01-17]
你错过了数组的最后一个元素的原因是你在从tri_nums弹出元素的同时递增n 。 在while循环的每次迭代开始时查看n和tri_nums的值: iteration n tri_nums n <= len(tri_nums) 0 0 [3, 2, 1] True 1 1 [2, 1] True 2 2 [1] False 您应该将n保持为0,并while tri_nums ( ... -
用Python中的numpy标识一列中具有相同值的向量(Identify vectors with same value in one column with numpy in python)[2023-07-04]
使用np.split : >>> a, b, c = np.split(orig, np.where(orig[:-1, 2] != orig[1:, 2])[0]+1) >>> a array([[1, 2, 3], [1, 2, 3]]) >>> b array([[1, 2, 4], [1, 2, 4], [1, 2, 4]]) >>> c array([[1, 2, 3], [1, 2, 3]]) Using np.split: >>> a ... -
python - 将numpy数组保存到文件(尽可能小的大小)(python - saving numpy array to a file (smallest size possible))[2021-08-10]
由于你有很多零,你只能写出表格中的非零元素(索引,数字)。 假设您有一个包含少量非零数字的数组: In [5]: a = np.zeros((10, 10)) In [6]: a Out[6]: array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., ... -
假设您沿着axis (即0或1)工作。 首先查找1的行数/列数: counts = (arr==1).sum(axis=axis) 您现在可以找到最小计数的索引,从计数列表中选择适当的计数,并将其除以所有其他计数: counts / counts[counts.argmin()] 请记住最小的数量可以是0。 Assume you work along the axis axis (which is 0 or 1). Start by finding the row/column counts of 1' ...
-
如何在Python 2.7中将列附加到2d numpy数组?(How to append a column to a 2d numpy array in Python 2.7?)[2022-12-03]
你可以使用numpy.column_stack : import numpy as np arr2d = np.matrix( [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) column_to_add = np.array([10, 40, 70, 100]) output = np.column_stack((arr2d, column_to_add)) 输出: matrix([[ 1, 2, 3, 10], [ ... -
在mysql中存储numpy数组(store numpy array in mysql)[2022-02-15]
您可以使用ndarray.dumps()将其pickle为字符串然后将其写入BLOB字段? 使用numpy.loads()恢复它 You could use ndarray.dumps() to pickle it to a string then write it to a BLOB field? Recover it using numpy.loads() -
p = p[~(p[:,2] > 30)] 或(如果您的情况很容易被逆转): p = p[p[:,2] <= 30] 回报 array([[ 0, 1, 3], [ 1, 5, 6], [ 1, 34, 4]]) p = p[~(p[:,2] > 30)] or (if your condition is easily inversible): p = p[p[:,2] <= 30] returns array([[ 0, 1, 3], [ ...