Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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}}
share|improve this question
5  
What is your expected result? –  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
1  
Your code, sam /. {x_, y_} -> {x -> {a_, b_} -> {a + b}, y /. {w_, e_} -> w^e} makes no sense to me. –  David Carraher Jul 18 '13 at 21:42
2  
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
1  
@MichaelE2: Please post your comment as answer so that I can accept and close this question. –  Rorschach Jul 19 '13 at 4:24

5 Answers 5

up vote 3 down vote accepted

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.

share|improve this answer
2  
+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.

share|improve this answer
    
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.

share|improve this answer

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

share|improve this answer
    
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. –  David Carraher 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. –  David Carraher 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}}
share|improve this answer
    
thanks for replying but I was looking for rules and replace options. –  Rorschach Jul 19 '13 at 4:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.