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?

Georgy
  • 12,464
  • 7
  • 65
  • 73
Thomas
  • 33
  • 1
  • 3

2 Answers2

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_]
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
  • 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). – Elis Byberi Nov 20 '17 at 18:03
  • You do realize that this is a perfectly valid and useful result? There is a reason for `arr[5:2]` not raising an exception. – Paul Panzer Nov 21 '17 at 01:06
  • The reason is that it is logically understandable. Slicing out of bounds return empty list. It may be invalid for GUI interface. – Elis Byberi Nov 21 '17 at 01:13
  • 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! – Elis Byberi Nov 21 '17 at 01:17
  • We are going off at a tangent, aren't we? ;-) – Paul Panzer Nov 21 '17 at 01:52
  • 1
    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! – Elis Byberi Nov 21 '17 at 01:59
  • Oxford begs to [differ](https://en.oxforddictionaries.com/definition/tangent) _"2 A completely different line of thought or action. ‘Loretta's mind went off at a tangent’"_ – Paul Panzer Nov 21 '17 at 02:13
  • Both are valid. I am sleeping. Good night! It is 03:28 AM in Tirana, Albania. – Elis Byberi Nov 21 '17 at 02:28
  • @PaulPanzer is there an alternative to the use of `eval`? Since you said that in this case it's suitable to use it, I was wondering if there's another way to achieve the result. Thanks – Aelius Jul 16 '21 at 13:09
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]
  • This would only work for use case a, not b (slicing with another array) – Thomas Nov 17 '17 at 14:15
  • 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 – Kishore Devaraj Nov 17 '17 at 14:21