MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: list manipulation

  • To: mathgroup at smc.vnet.net
  • Subject: [mg100164] Re: [mg100131] Re: [mg100122] list manipulation
  • From: DrMajorBob <btreat1 at austin.rr.com>
  • Date: Wed, 27 May 2009 04:04:58 -0400 (EDT)
  • References: <200905191101.HAA07313@smc.vnet.net> <gv0gn0$a19$1@smc.vnet.net>
  • Reply-to: drmajorbob at bigfoot.com

Like this?

n = 10;
list = {Array[a, n], Array[b, n], Array[c, n]};
f @@@ Transpose@list

{f[a[1], b[1], c[1]], f[a[2], b[2], c[2]], f[a[3], b[3], c[3]],
  f[a[4], b[4], c[4]], f[a[5], b[5], c[5]], f[a[6], b[6], c[6]],
  f[a[7], b[7], c[7]], f[a[8], b[8], c[8]], f[a[9], b[9], c[9]],
  f[a[10], b[10], c[10]]}

Bobby

On Tue, 26 May 2009 04:07:25 -0500, David Park <djmpark at comcast.net> 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}}
>
>
>



-- 
DrMajorBob at bigfoot.com


  • Prev by Date: Re: Keep Slider Consistent With a Slow Graph
  • Next by Date: Re: Re: 100,000 posts!
  • Previous by thread: Re: list manipulation
  • Next by thread: Re: list manipulation