3

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 be used for the calculation at hand.

Simple example of what I'd like to do:

arr = array([0, 10, 20, 30, 40, 50, 60, 70, 80, 90])

a = "2:4" # example string from GUI Textbox
b = "[3, 4, 5]" # example string from GUI Textbox


print arr[a] # not valid code -> what should be written here to make it work?
print arr[b] # not valid code -> what should be written here to make it work?

should output:

[20, 30]
[30, 40, 50]

I found out about the slice function, but I'd need to parse my string manually and create a slice. Is there a simpler way?

2
  • is this expected to handle only 1d arrays? Commented Nov 17, 2017 at 15:59
  • No, arbitrary dimension (up to fifth at this point) Commented Nov 19, 2017 at 11:11

2 Answers 2

2

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

if not all(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_]
Sign up to request clarification or add additional context in comments.

9 Comments

It is important to note that if start is bigger than stop, it will return an empty array. arr[5:2] is empty and no exception is raised. That's why I was talking about security (implying validity).
You do realize that this is a perfectly valid and useful result? There is a reason for arr[5:2] not raising an exception.
The reason is that it is logically understandable. Slicing out of bounds return empty list. It may be invalid for GUI interface.
You do realize that f-string f'np.s_[{b}]' can not be used in Python 2.7? I was wondering how OP got this working! Hahahaha!
It is "go off on a tangent". However, I do not know what in particular you do not understand in my statement: arr[5:2] is empty and no exception is raised. Your question is redundant: You do realize that this is a perfectly valid and useful result? I am out! Have a nice day!
|
0

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]

2 Comments

This would only work for use case a, not b (slicing with another array)
If these two are your only inputs, then you should have a logic in place to identify whether it is array or slicing type. Then you can add a if else condition to process your data. For array type, the same code will work, but just increment your max value by one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.