trialList = {{0, 24.701}, {1, 24.69}, {2, 24.733}};
f[x_] := x + 1;
I would like to apply the function f to only the second element of each tuple. How is this done?
The actual list is 16,000 entries long. How do I iterate over the whole list applying the function to only the second entry of each tuple? I tried the following, but it does not do what I want:
{trialList[[1]], f[trialList[[2]]]} & @@@ trialList
Answer
If you need to worry about execution speed, then there's a better alternative, using Transpose. First I'll list the timings for the solutions in the other answers. For that purpose, I create a trialList with 160000 random entries, and in the tests I suppress the output to avoid huge output lists:
trialList = RandomReal[{-1, 1}, {160000, 2}];
f[x_] := x + 1
Timing[{#1, f[#2]} & @@@ trialList;]
{0.326740, Null}
Timing[MapAt[f, trialList, {;; , 2}];]
{0.271610, Null}
Timing[trialList[[All, 2]] = f /@ trialList[[All, 2]];]
{0.207944, Null}
Timing[trialList /. {a_, b_} :> {a, f[b]};]
{0.290535, Null}
Here is my proposal:
Timing[Transpose[{#1, Thread[f[#2]]} & @@ Transpose[trialList]];]
{0.053719, Null}
It's 4 to 6 times faster. The right-most Transpose converts the trialList into a list with only two entries, each of which has 160000 elements. The first list contains all the first members of the original tuples, and the second list the second members. The @@ to the left of Transpose says to Apply to this list a function which is {#1, Thread[f[#2]]} &. It has two formal arguments #1 and #2, corresponding to the two large lists. Only the second argument is modified by Threading your function f over it (i.e., applying it to all the elements of the list #2). The result is two lists, of which only the second differs from the original. By doing another Transpose we get the original tuple structure back.
Comments
Post a Comment