Multiple level lists

Let there be a list,

Sample={{{1, 2, 3}, {a, b, c}}, {{2, 3, 4}, {d, f, g}}}


I want to go to individual sublists and do some operation on it.I tried a few things like rule and replace.

sam /. {x_, y_} -> {x -> {a_, b_} -> {a + b}, y /. {w_, e_} -> w^e}


but I get

{{{1, 2, 3}, {a, b, c}} -> {a_, b_} -> {a + b}, {{2, 3, 4}, {d, f, g}}}


But I am not getting to individual lists. I want to use only rule or replace.

Edit: To make my question more clear I want to specify that I am not interested in one particular list operation. My idea is to be able to traverse sublists using Rule or Replace and then work on it. My expected result is like

{{{1 + a, 2 + b, 3 + c}}, {2^d, 3^f, 4^g}}

-
What is your expected result? – Dr. belisarius Jul 18 '13 at 20:12
Does this come close? sam /. {x_List, y_List} :> {Total[x], y[[1]]^y[[2]]} I am not sure, the expected result would help. – chuy Jul 18 '13 at 20:13
Your code, sam /. {x_, y_} -> {x -> {a_, b_} -> {a + b}, y /. {w_, e_} -> w^e} makes no sense to me. – DavidC Jul 18 '13 at 21:42
Perhaps you meant RuleDelayed: sam /. {x_, y_} :> {x /. {a_, b_} :> {a + b}, y /. {w_, e_} :> w^e}? (And a /. in place of one of the ->.) – Michael E2 Jul 18 '13 at 23:27
@MichaelE2: Please post your comment as answer so that I can accept and close this question. – Rorschach Jul 19 '13 at 4:24

Here is my answer from my comment, as kindly requested... Perhaps you meant RuleDelayed:

sam = {{{1, 2, 3}, {a, b, c}}, {{2, 3, 4}, {d, f, g}}}
sam /. {x_, y_} :> {x /. {a_, b_} :> {a + b}, y /. {w_, e_} :> w^e}
(* {{{1 + a, 2 + b, 3 + c}}, {2^d, 3^f, 4^g}} *)


In general given two operations f, g, one might do

sam /. {x_, y_} :> {x /. {a_, b_} :> f[a, b], y /. {w_, e_} :> g[w, e]}


Please note that Mr. Wizard has rather generously extended this one method to several alternatives, each of which might be more suitable to a given situation. Certainly they are instructive in any case.

-
+1 since you were here first, even if the question was closed at the time. :-) – Mr.Wizard Jul 20 '13 at 1:43

Based on your updated question you could use any of these:

sample /. {a_, b_} :> {Plus @@ a, Power @@ b}

sample /. {{a_, b_}, {c_, d_}} :> {a + b, c^d}

sample /. {a_, b_} :> {a /. {i_, j_} :> i + j, b /. {i_, j_} :> i^j}

ReplacePart[sample, {{1, 0} -> Plus, {2, 0} -> Power}]


Every line produces:

{{1 + a, 2 + b, 3 + c}, {2^d, 3^f, 4^g}}


These don't use replacement rules but I think they are worth a look nevertheless:

s2 = sample; s2[[All, 0]] = {Plus, Power}; s2

{Plus @@ #, Power @@ #2} & @@ sample

MapIndexed[{Plus, Power}[[First@#2]] @@ # &, sample]

fn := (fn = Power; Plus); fn @@@ sample


See What does the construct f[x_] := f[x] = ... mean? for an explanation of the last one.

-
thanks for collective information...! – Rorschach Jul 20 '13 at 13:26

I would try something like:

DeleteCases[Cases[Sample, _List, {-2}], {}]


which gives you:

{{1, 2, 3}, {a, b, c}, {2, 3, 4}, {d, f, g}}


but unfortunately this method doesn't use rules.

-

Just a guess that you may want something like the following:

sample = {{{1, 2, 3}, {a, b, c}}, {{2, 3, 4}, {d, f, g}}};
h[x_] := x /. {n_Integer :>   q[n], z_Symbol :> r[z]}
Map[h, sample, {3}]


Mapassigns the function h (replacement rules) to each element at level 3.

{{{q[1], q[2], q[3]}, {r[a], r[b], r[c]}}, {{q[2], q[3], q[4]}, {r[d], r[f], r[g]}}}

-
You can use Replace[sample, {n_Integer :> q[n], z_Symbol :> r[z]}, {3}] to fulfil the only thing that OP has stated clearly. But I'm still not sure if this is what OP wants. How it is possible that this question has +2? Am I missing something? ;) – Kuba Jul 18 '13 at 22:17
Thanks for the tip. I didn't know that replace could specify levels. Since you suggested Replace, you may wish to submit it as your conjecture. – DavidC Jul 18 '13 at 22:33
You are welcome :). Notice that above replacements do not do the same thing what your Map[h... does. It matches only in this case. My point was to show levelspec for Replace ;) – Kuba Jul 18 '13 at 22:46
Yes, that's how I understood your comment. The OP's issue seems to be with acting at the correct level. – DavidC Jul 18 '13 at 22:53
@DavidCarraher:david thanks for taking time to write but I was looking forward to first traverse to smallest level and than work on it. – Rorschach Jul 19 '13 at 4:20

You can just use Flatten:

Sample = {{{1, 2, 3}, {a, b, c}}, {{2, 3, 4}, {d, f, g}}}


and then

final=Flatten[Sample, 1]


Which you get:

{{1, 2, 3}, {a, b, c}, {2, 3, 4}, {d, f, g}}

-
thanks for replying but I was looking for rules and replace options. – Rorschach Jul 19 '13 at 4:19