【发布时间】:2017-11-17 13:25:40
【问题描述】:
我正在通过 GUI 在 python 中处理大型数值数组。 我想将切片功能暴露给 GUI 中的文本框,这样我就可以轻松地选择应该用于手头计算的数组部分。
我想做的简单例子:
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?
应该输出:
[20, 30]
[30, 40, 50]
我发现了 slice 函数,但我需要手动解析我的字符串并创建一个 slice。有没有更简单的方法?
【问题讨论】:
-
这是否应该只处理一维数组?
-
不,任意维度(此时最多为第五个)