Re: list manipulation

• To: mathgroup at smc.vnet.net
• Subject: [mg100132] Re: [mg100122] list manipulation
• From: "David Park" <djmpark at comcast.net>
• Date: Tue, 26 May 2009 05:07:36 -0400 (EDT)
• References: <200905191101.HAA07313@smc.vnet.net> <gv0gn0\$a19\$1@smc.vnet.net> <20101854.1243249205388.JavaMail.root@n11>

```The Presentations package from my web site (\$50) has a command in the
Manipulations section named MapLevelParts that does what you want.

MapLevelParts[function, {topposition, levelpositions}][expr] will map the
function onto the selected level positions in an expression. Levelpositions
is a list of the selected parts. The function is applied to them as a group
and they are replaced with a single new expression. Other parts not
specified in levelpositions are left unchanged.
Example:
a + b + c + d + e // MapLevelParts[f, {{2,4,5}}] -> a + c + f[b + d + e]

For your case we could do it as follows:

testlist = RandomInteger[{-10, 10}, {5, 5}]
{{6, 4, 5, -5, -2}, {-3, -5, -8, 9, -3}, {6, -2, -3, 8, -4}, {4, 10,
2, -6, 9}, {-2, -7, 4, 4, -7}}

MapLevelParts[f @@ # &, {{3, 4}}] /@ testlist
{{6, 4, f[5, -5], -2}, {-3, -5, f[-8, 9], -3}, {6, -2,
f[-3, 8], -4}, {4, 10, f[2, -6], 9}, {-2, -7, f[4, 4], -7}}

David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/

From: Scot T. Martin [mailto:smartin at seas.harvard.edu]

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}}

```

• Prev by Date: Re: Keep Slider Consistent With a Slow Graph
• Next by Date: Re: Logscale Tick Function
• Previous by thread: Re: list manipulation
• Next by thread: Re: list manipulation