楼主:
abram (科科)
2019-11-13 19:18:29不好意思,因为用习惯了 Mathematica 的 Position 指令,
所以小弟正在用 Python 写一个执行相同功能的指令。
现在卡住的点是,如果知道输入的 List 是二维的,
当然就是用两层的 for loop:
def position(arr,cri):
index=[];
for i in range(len(x)):
for j in range(len(x[i])):
if x[i][j] == cri:
index.append([i, j])
return index
相对地,若知道输入 List 是三维的当然就用三层的 loop。
可是当程式处理的维度由输入 List 内生决定时,例如在
Mathematica 可以执行:
Position[{2, {1, 2}, {{1, 1, 2}}}, 2]
得到结果为:
{{1}, {2, 2}, {3, 1, 3}}
不知道要怎么写才能在 Python 下实作类似的功能呢?
谢谢!