Skip to content Skip to sidebar Skip to footer

Using String As Array Indices In Numpy

I'm handling large numerical arrays in python through a GUI. I'd like to expose the slicing capabilities to a textbox in a GUI, so I can easily choose part of the array that should

Solution 1:

Maybe since you are only expecting a very limited character set it is acceptable using eval this once:

ifnotall(c in"1234567890-[],: "for c in b): # maybe also limit the length of b?# tell user you couldn't parse and exit this branch
slice_ = eval(f'np.s_[{b}]')
# slice_ can now be applied to your array: arr[slice_]

Solution 2:

Try this i think it works, assuming that you will get min and max in your string.

import re
a = "2:4"min = int(min(re.findall(r'(\d)',a)))
max = int(max(re.findall(r'(\d)',a)))
print arr[min:max]

Post a Comment for "Using String As Array Indices In Numpy"