5
$\begingroup$

What is Mathematica's equivalent to Python's ZIP command? Is this called nesting? It means iterating over sub-sets of values like the below example. How can I do it in Mathematica?

Example in Python that I want in Mathematica, I want the ZIP!

>>> [(ii,jj) for (ii,jj) in zip((1,10,100),(2,20,200))]
[(1, 2), (10, 20), (100, 200)]
>>> [(ii,jj,kk) for (ii,jj,kk) in zip((1,10,100),(2,20,200),(3,30,300))]
[(1, 2, 3), (10, 20, 30), (100, 200, 300)]

[Update] question got solved, Python's Zip is Transpose in Mathematica. The real challenge is now how to operate it like Spherical-Cartesian conversion below

**Mathematica Example: input as spherical (L,Azimuth,Phi) and output as cartesian (x,y,z), how?**

    L = {874, 4513, 1487};
    A = {120 Degree, 140 Degree, 180 Degree};
    Inc = {14.1 Degree, 66.2 Degree, 66.2 Degree};
    tuple = Transpose[{L, A, Inc}];
    hhh = Print[N[{#1*Cos[#2], #1*Sin[#2]*Cos[#3], #1*Sin[#2]*Sin[#3]}]] &[r, \[Theta], \[Phi]];
    hhh @@ tuple
$\endgroup$

closed as off-topic by eldo, Öskå, Yves Klett, C. E., ybeltukov Sep 23 '14 at 12:42

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Yves Klett, C. E., ybeltukov
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    $\begingroup$ Check Map and Transpose. Or maybe Thread does what you want? In[135]:= Thread[f[{1, 10, 100}, {2, 20, 200}, {3, 30, 300}]] Out[135]= {f[1, 2, 3], f[10, 20, 30], f[100, 200, 300]} $\endgroup$ – Daniel Lichtblau Sep 22 '14 at 22:46
  • 1
    $\begingroup$ Why did you Thread[Times[...]] instead of threading List? $\endgroup$ – Daniel Lichtblau Sep 22 '14 at 23:11
  • 1
    $\begingroup$ As noted above, Thread is the equivalent to zip. MapThread provides control similar to list comprehension. $\endgroup$ – mfvonh Sep 22 '14 at 23:20
  • 3
    $\begingroup$ MapThread[Function[{x,y,x},x+y+z],{xlist,ylist,zlist}] is pretty darn close to [x+y+z fir x,y,z in zip(xlist,ylist,zlist)] $\endgroup$ – george2079 Sep 23 '14 at 0:25
  • 2
    $\begingroup$ @hhh it isn't working because you do not have the correct syntax for [Function, which is Function[{args},CoordinateTransform[(* etc *)]][args]. Note the [args] at the end. I'd suggest using the pure function form if you are going to put it in a MapThread. You do not need to be a "super guru", you just need to pay attention to the documented syntax. $\endgroup$ – Verbeia Sep 23 '14 at 2:22
11
$\begingroup$

The function you want is MapThread - no Transpose needed.

MapThread[f[#1, #2, #3] &, {L, A, Inc}]

(* {f[874, 120 \[Degree], 0.246091], f[4513, 140 \[Degree], 1.15541], 
 f[1487, 180 \[Degree], 1.15541]} *)

And for your function:

MapThread[N[{#1*Cos[#2], #1*Sin[#2]*Cos[#3], #1*Sin[#2]*Sin[#3]}] &, 
 {L, A, Inc}]
(* {{-437., 734.102, 184.394}, {-3457.16, 1170.64, 2654.21}, {-1487., 0., 0.}} *)

EDIT: And for your other function - note the use of SlotSequence:

SpherCat = CoordinateTransform["Spherical" -> "Cartesian", {##}] &

(* CoordinateTransform["Spherical" -> "Cartesian", {##1}] & *)

MapThread[SpherCat, {L, A, Inc}]

(* CoordinateTransform::bdpt: Evaluation point {1487,180 \[Degree],1.15541} is 
incompatible with the coordinate assumptions of the specified coordinate chart. >> *)

{{734.102, 184.394, -437}, {1170.64, 
  2654.21, -4513 Cos[40 \[Degree]]}, 
 CoordinateTransform[
  "Spherical" -> "Cartesian", {1487, 180 \[Degree], 1.15541}]} *)

There seems to be an error in your input (perhaps you intended the reverse transformation?) but the question of how to simply and elegantly operate on the lists you want is dealt with.

$\endgroup$
  • $\begingroup$ Can you elaborate this with another function such as the ready CoordinateTransform where you don't specify the & explicitly? How to use MapThread with it? I am trying to do it with it now to replace the self-done function here. I must say that I am so confused to Mathematica, why on earth they cannot get the shxt out and design the syntax clean (sorry just frustrated in trying to understand a simple thing like tuple manipulation). $\endgroup$ – hhh Sep 23 '14 at 1:38
  • $\begingroup$ I did it in two different ways. I got one method working but MapThread not working i.stack.imgur.com/ySiIZ.png, trying... $\endgroup$ – hhh Sep 23 '14 at 1:56
  • 1
    $\begingroup$ @hhh, as mentioned in my other comment, that's nothing to do with MapThread and everything to do with incorrect syntax for the full form of Function. Also kindly stop moving the goal posts by revising the question and function of interest. No wonder this question has been downvoted. $\endgroup$ – Verbeia Sep 23 '14 at 2:23
  • $\begingroup$ I like the fact that you are never giving up +1, real hxcker attitude! Sometimes it is muddy but always fighting to the goal. Thank you. $\endgroup$ – hhh Sep 23 '14 at 3:18
7
$\begingroup$

You could get the lists you want simply by entering them as a nested list and transposing it:

In[60]:= Transpose@{{1, 10, 100}, {2, 20, 200}}

Out[60]= {{1, 2}, {10, 20}, {100, 200}}

and

In[61]:= Transpose@{{1, 10, 100}, {2, 20, 200}, {3, 30, 300}}

Out[61]= {{1, 2, 3}, {10, 20, 30}, {100, 200, 300}}
$\endgroup$
  • $\begingroup$ Actually, you are right in this +1 -- took me a long time and Freenode gurus to hit me a little! Transpose is the equivalent to Python's Zip -- dxmn I got a new question here. The real challenge is how to operate the tuples in Mathematica by the Spherical-Cartesian function. Somehow with ## and &...ideas? $\endgroup$ – hhh Sep 22 '14 at 23:52
  • $\begingroup$ zip[{} ..] := {}; zip [p : xs__] /; MemberQ[{p}, {}] := {}; zip[p : Repeated[_List, {2, \[Infinity]}]] := Join[Through[{Apply[First[{#}]&, {##}, {1}]&}[p]], zip[(Rest[{##1}] & @@@ {p}) /. {x__List} :> Sequence[x]]] one can use this implementation as well for sub-lists of same or different lengths $\endgroup$ – Ali Hashmi Aug 18 '17 at 22:52
1
$\begingroup$

Here's an answer to the question you have now erased.

questions = {"name", "quest", "favorite color"}; 
answers = {"lancelot", "the holy grail", "blue"};
"What is your " <> questions[[#]] <> "? It is " <> answers[[#]] <> "." & /@ Range[3]

{"What is your name? It is lancelot.", "What is your quest? It is the holy grail.", "What is your favorite color? It is blue."}

$\endgroup$
  • $\begingroup$ Err I provided an easier example to make question easier to understand, could you update? I felt this example was not as clear as the latter one. There must be something as succint as in Python built in to Mathematica, always trying to find the most elegant solution. $\endgroup$ – hhh Sep 22 '14 at 22:38
-1
$\begingroup$

God in Freenode helped to find some material related to this one, important to become more self-aware of things! Tuple ordering is not trivial thing. Starting the succicnt summary here!

Summary

  • Pair lists to create tuples in order covering ragged tuple ordering, Python's zip can be implemented with many different methods such as Flatten for ragged case and Transpose for non-ragged case
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.