6
$\begingroup$

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}}
$\endgroup$
  • 5
    $\begingroup$ What is your expected result? $\endgroup$ – Dr. belisarius Jul 18 '13 at 20:12
  • $\begingroup$ Does this come close? sam /. {x_List, y_List} :> {Total[x], y[[1]]^y[[2]]} I am not sure, the expected result would help. $\endgroup$ – chuy Jul 18 '13 at 20:13
  • 1
    $\begingroup$ Your code, sam /. {x_, y_} -> {x -> {a_, b_} -> {a + b}, y /. {w_, e_} -> w^e} makes no sense to me. $\endgroup$ – DavidC Jul 18 '13 at 21:42
  • 2
    $\begingroup$ 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 ->.) $\endgroup$ – Michael E2 Jul 18 '13 at 23:27
  • 1
    $\begingroup$ @MichaelE2: Please post your comment as answer so that I can accept and close this question. $\endgroup$ – Sejwal Jul 19 '13 at 4:24
3
$\begingroup$

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.

| improve this answer | |
$\endgroup$
  • 2
    $\begingroup$ +1 since you were here first, even if the question was closed at the time. :-) $\endgroup$ – Mr.Wizard Jul 20 '13 at 1:43
5
$\begingroup$

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.

| improve this answer | |
$\endgroup$
  • $\begingroup$ thanks for collective information...! $\endgroup$ – Sejwal Jul 20 '13 at 13:26
2
$\begingroup$

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.

| improve this answer | |
$\endgroup$
2
$\begingroup$

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

| improve this answer | |
$\endgroup$
  • $\begingroup$ 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? ;) $\endgroup$ – Kuba Jul 18 '13 at 22:17
  • $\begingroup$ 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. $\endgroup$ – DavidC Jul 18 '13 at 22:33
  • $\begingroup$ 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 ;) $\endgroup$ – Kuba Jul 18 '13 at 22:46
  • $\begingroup$ Yes, that's how I understood your comment. The OP's issue seems to be with acting at the correct level. $\endgroup$ – DavidC Jul 18 '13 at 22:53
  • $\begingroup$ @DavidCarraher:david thanks for taking time to write but I was looking forward to first traverse to smallest level and than work on it. $\endgroup$ – Sejwal Jul 19 '13 at 4:20
1
$\begingroup$

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}}
| improve this answer | |
$\endgroup$
  • $\begingroup$ thanks for replying but I was looking for rules and replace options. $\endgroup$ – Sejwal Jul 19 '13 at 4:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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