3
$\begingroup$

I have a list in the form {{a, 1}, {b, 2}, {c, 3}}.

Let's say I want to operate a transformation on all the second elements of each sublist, but this transformation is a function of them all together. As an example I want to Standardize them.

At the moment I've implemented this with Transpose.

mylist = {{a, 1}, {b, 2}, {c, 3}};
newlist = Transpose[{mylist[[All, 1]], Standardize[mylist[[All, 2]]]}]

and the output is correctly

{{a, -1}, {b, 0}, {c, 1}}

I find this a little cumbersome, this approach can become complicated and hard to read sometimes. Isn't there a more natural way?

$\endgroup$
  • 2
    $\begingroup$ You could do newlist = mylist; newlist[[All, 2]] = Standardize[newlist[[All, 2]]];, but I don't consider it any more natural than what you have. $\endgroup$ – J. M.'s technical difficulties Jun 10 '13 at 15:30
  • $\begingroup$ I am positive this question is a duplicate, though perhaps the original is on StackOverflow. I'd appreciate help finding it. $\endgroup$ – Mr.Wizard Jun 10 '13 at 15:32
  • $\begingroup$ This is related but it does not expressly describe passing the entire column to the function; I know there is a question that does. $\endgroup$ – Mr.Wizard Jun 10 '13 at 15:37
  • 1
    $\begingroup$ Another slight variation (but hardly more natural either): MapAt[Standardize, #\[Transpose], 2]\[Transpose] &@mylist $\endgroup$ – user1066 Jun 10 '13 at 16:54
  • $\begingroup$ Duh, deleting my remark, which is just a repeat of what 0x4A4D said. But I like that method. $\endgroup$ – Todd Gayley Jun 10 '13 at 18:18
1
$\begingroup$

I'd suggest...

Apply[Transpose[{#1,Standardize[#2]}]&,Transpose[mylist]]
|improve this answer|||||
$\endgroup$
0
$\begingroup$

Here's a small variation on the solutions proposed in the comments:

Thread[{First[Thread[myList]], Standardize[Last[Thread[myList]]]}]
|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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