As a python programmer, numpy tools usually come to my mind if I want to manipulate arrays (list, matrices, ...). Is there any reference (e.g., dictionary) of how to translate numpy syntax to Mathematica syntax? For instance, assuming a
is an array, I found the following translations (python code as quotes, Mathematica code as code):
- linspace / logspace
a = np.linspace(0,10,11)
a = Array[# &, 11, {0, 10}]
(* {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} *)
- element selection based on condition
a[a>0]
Select[a, # > 0 &]
(* {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} *)
- element selection based on other boolean array
s = a>0
a[s]
s = # > 0 & /@ a
Pick[a, s]
(* {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} *)
a[3:-2:2]
a[[4;;-3;;2]]
(* {3, 5, 7} *)
Such a correspondence list is what I am looking for. Does this already exist? If not, I believe it would be helpful to extend this Q&A post to continuously build such a list.
One thing I am trying to use just now is np.roll
. Using the example from above, I am searching for a command that yields
EquivalentOfNpRoll[a,2]
(* {9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8}
How would I do this? Permute
seems to be an overkill. At least it was far from obvious to me how to implement this rolling permutation in a general but compact way.
Update:
From the comments:
np.roll(a,2)
RotateRight[a, 2]
(* {9, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8} *)
a=np.arange(0,11,1)
a = Range[0, 10, 1]
Comments
Post a Comment