3
$\begingroup$

Suppose that I have a list:

G = {{{a, c}, {a, c, x}}, {{c, e}, {c, e, x}}, {{e, f}, {a, e, f}}, 
     {{f, g}, {a, f, g}}, {{g, a}, {a, f, g}}, {{a, c, x}, {a, c}}, 
     {{a, c, x}, {a, e, x}}, {{a, c, x}, {c, e, x}}, {{a, e, f}, {e, f}}, 
     {{a, e, f}, {a, e, x}}, {{a, e, f}, {a, f, g}}, {{a, e, x}, {a, c, x}}, 
     {{a, e, x}, {a, e, f}}, {{a, e, x}, {c, e, x}}, {{a, f, g}, {f, g}}, 
     {{a, f, g}, {g, a}}, {{a, f, g}, {a, e, f}}, {{c, e, x}, {c, e}}, 
     {{c, e, x}, {a, c, x}}, {{c, e, x}, {a, e, x}}}

in which each element of G is an ordered pair {A,B}, where A and B are sets. I would like to convert the elements A and B into the numbers such as {a, c} -> 1, {c, e} -> 2, ..., {a, c, x} -> 10. I also need to change all of {c, e} in G into 2. How can I do this? I try to use Replace but it doesn't work.

$\endgroup$
  • 1
    $\begingroup$ just curious: why is {a,c,x}->10? Maybe you could implement something smarter to replace (but I cannot see a pattern). Also, you should try to avoid single, capitalized letters as variable names (to avoid conflicts) $\endgroup$ – Pinguin Dirk Apr 30 '13 at 7:26
3
$\begingroup$

ReplaceRepeated is probably what you want. Something like:

G //. {{a, c} -> 1, {a, c, x} -> 2}

//. is the shortcut for this. ReplaceAll probably also works:

G /. {{a, c} -> 1, {a, c, x} -> 2}
$\endgroup$

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.