Python 如何返回包含已删除值的新数组
我尝试在不使用第一个数组中的2的情况下获得新数组,当然有很多方法可以实现,但我正在寻找更少的编码和更智能的解决方案,例如此数组..:Python 如何返回包含已删除值的新数组,python,arrays,python-3.x,numpy,Python,Arrays,Python 3.x,Numpy,我尝试在不使用第一个数组中的2的情况下获得新数组,当然有很多方法可以实现,但我正在寻找更少的编码和更智能的解决方案,例如此数组..: somearray = np.array( [4, 5, 2, 8, 4, 7, 2, 64, 2, 57, 2, 45, 7, 43, 2, 5, 7, 3, 3, 6523, 3, 4, 3, 0, -65, -343]) #so create array for deleted 2's for the new array only, the first on
somearray = np.array( [4, 5, 2, 8, 4, 7, 2, 64, 2, 57, 2, 45, 7, 43, 2, 5, 7, 3, 3, 6523, 3, 4, 3, 0, -65, -343])
#so create array for deleted 2's for the new array only, the first one will not be affected.
newarray = ??
请不要回答我“np.ma.masked_数组(vec,vec==2)
”它将用--
替换2而不删除它们。您可以使用方法在数组中查找2的索引:
indices=np.argwhere(somearray==2)
现在,最后使用方法:
现在,如果您打印newarray
,您将获得所需的输出:
array([ 4, 5, 2, 8, 4, 7, 64, 57, 45, 7, 43,
5, 7, 3, 3, 6523, 3, 4, 3, 0, -65, -343])
使用正则表达式查找所有相等的值2并忽略
import re
original_array = np.array( [4, 5, 2, 8, 4, 7, 2, 64, 2, 57, 2, 45, 7, 43, 2, 5, 7, 3, 3, 6523, 3, 4, 3, 0, -65, -343])
digit_re = re.compile('[2]{1}')
[print (x) for x in original_array if not digit_re.search(str(x))]
好吧,你是对的,作为一个新的python开发人员,我猜这个问题是重复的,不管你得到了什么正确答案+1.
import re
original_array = np.array( [4, 5, 2, 8, 4, 7, 2, 64, 2, 57, 2, 45, 7, 43, 2, 5, 7, 3, 3, 6523, 3, 4, 3, 0, -65, -343])
digit_re = re.compile('[2]{1}')
[print (x) for x in original_array if not digit_re.search(str(x))]