[Solved] RegEx matching for removing a sequence of variable length in a number

I assume X and Y can’t begin with 0. [1-9]\d{0,2} matches a number from 1 to 3 digits that doesn’t begin with 0. So the regexp to extract X and Y should be: ^([1-9]\d{0,2})000([1-9]\d{0,2})000$ Then you can use re.sub() to remove the zeroes between X and Y. regex = re.compile(r’^([1-9]\d{0,2})000([1-9]\d{0,2})000$’); i = 14000010000 istr = … Read more

[Solved] What is the shortest way to calculate running median in python?

This is the shortest: from scipy.ndimage import median_filter values = [1,1,1,0,1,1,1,1,1,1,1,2,1,1,1,10,1,1,1,1,1,1,1,1,1,1,0,1] print median_filter(values, 7, mode=”mirror”) and it works correctly in the edges (or you can choose how it works at the edges). And any general running X is done like this (running standard deviation as an example): import numpy from scipy.ndimage.filters import generic_filter values = … Read more

[Solved] Change color in RGB images

I have to agree with Majid Shirazi regarding the proposed solution by Quang Hong. Let’s have a look at: idx = np.where(img == (0.4, 0.4, 0.4)) Then, idx is a 3-tuple containing each a ndarray for all x-coordinates, y-coordinates, and “channel”-coordinates. From NumPy’s indexing, I can’t see a possibility to properly access/manipulate the values in … Read more

[Solved] Python performance [closed]

I made a great experience with Cython (Another thing than CPython…). You can make plain C code out of your program, compile it to an extension and just run it from another Python file. See this question from me for more information on building an extension including numpy: How to create a .pyd file?. Sample: … Read more

[Solved] Count occurences of all items of a list in a tuple

Being NumPy tagged, here’s a NumPy solution – In [846]: import numpy as np In [847]: t = (1,5,2,3,4,5,6,7,3,2,2,4,3) In [848]: a = [1,2,3] In [849]: np.in1d(t,a).sum() Out[849]: 7 # Alternatively with np.count_nonzero for summing booleans In [850]: np.count_nonzero(np.in1d(t,a)) Out[850]: 7 Another NumPy one with np.bincount for the specific case of positive numbered elements in … Read more

[Solved] how to convert a np array of lists to a np array

I was wondering how you got the array of lists. That usually takes some trickery. In [2]: >>> a = np.array([“0,1”, “2,3”, “4,5”]) …: >>> b = np.core.defchararray.split(a, sep=’,’) …: In [4]: b Out[4]: array([list([‘0’, ‘1’]), list([‘2’, ‘3’]), list([‘4’, ‘5’])], dtype=object) Simply calling array again doesn’t change things: In [5]: np.array(b) Out[5]: array([list([‘0’, ‘1’]), list([‘2’, … Read more