Re: list manipulation
- To: mathgroup at smc.vnet.net
 - Subject: [mg100138] Re: [mg100122] list manipulation
 - From: Leonid Shifrin <lshifr at gmail.com>
 - Date: Tue, 26 May 2009 05:08:45 -0400 (EDT)
 - References: <200905191101.HAA07313@smc.vnet.net> <gv0gn0$a19$1@smc.vnet.net>
 
Hi Scot,
I would not call your solutions  clumsy. I think they are brief, reasonably
fast and accomplish their goal. Note that your second solution won't work in
the way you put it - you also need Replace, otherwise the matching will
happen on a higher level  - f will be applied to the 4-th and 5-th sublists
of your main list (you did not observe it since you only had 2 sublists).
Also, I think  you need there RuleDelayed rather than Rule:
In[1] =
data = {{element11, element12, element1N, target11, target12,
    element1N3, element1N4}, {element21, element22, element2N,
    target21, target22, element2N3, element2N4}};
In[2] = Replace[data, {a : Repeated[_, {3}], b : Repeated[_, {2}],
   c : __} :> {a, f[b], c}, {1}]
Out[2] = {{element11, element12, element1N, f[target11, target12],
element1N3,   element1N4}, {element21, element22, element2N,
  f[target21, target22], element2N3, element2N4}}
Note also that they are not equivalent: your first one creates f[{a,b}]
while
the second one just f[a,b].
Here is  a little more obscure but faster (about 4 x times, according to my
benchmarks)  way to accomplish your goal (assuming the format of your second
solution):
In[3] =
Module[{copy = data},
 copy[[All, 4]] = f @@@ copy[[All, {4, 5}]];
 copy[[All, 5]] = Sequence[];
 copy]
Out[3] =
{{element11, element12, element1N, f[target11, target12], element1N3,
  element1N4}, {element21, element22, element2N,
  f[target21, target22], element2N3, element2N4}}
Hope this helps.
Regards,
Leonid
On Mon, May 25, 2009 at 3:18 AM, Scot T. Martin <smartin at seas.harvard.edu>wrote:
> I'm wondering if anyone has bright ideas on the puzzle below. I'm trying
> to take a very large list of the following form:
>
> {{.., .., a, b, ..},{.., .., c, d, ..},..}
>
> into the form
>
> {{.., .., f[a, b], ..},{.., .., f[c, d], ..},..}
>
> It seems to me that there must be some clever and beautiful way to do this
> that I haven't thought of. My code is below with two clumsy solutions.
>
> Suggestions on how to do better?
>
> My code:
>
> In[1]:= (*shown here as minimal to define problem but actual list has \
> large N and many entries*)
>
> data = {{element11, element12, element1N, target11, target12,
>     element1N3, element1N4}, {element21, element22, element2N,
>     target21, target22, element2N3, element2N4}};
>
>
>
> In[2]:= (* first clumsy approach *)
>
> Replace[data, list : _List :> {Sequence @@ list[[1 ;; 3]],
> Sequence[f[list[[4 ;; 5]]]], Sequence @@ list[[6 ;;]]}, {1}]
>
>
> Out[2]= {{element11, element12, element1N, f[{target11, target12}],
>   element1N3, element1N4}, {element21, element22, element2N,
>   f[{target21, target22}], element2N3, element2N4}}
>
>
>
> In[3]:= (* second clumsy approach *)
>
> data /. {a : Repeated[_, {3}], b : Repeated[_, {2}],
> c : __} -> {a, f[b], c}
>
> Out[3]= {{element11, element12, element1N, f[target11, target12],
>   element1N3, element1N4}, {element21, element22, element2N,
>   f[target21, target22], element2N3, element2N4}}
>
>
- References:
- Are you wolfing tonight?
- From: magma <maderri2@gmail.com>
 
 
 - Are you wolfing tonight?