Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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) – Pinguin Dirk Apr 30 '13 at 7:26
up vote 3 down vote accepted

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}
share|improve this answer
Thank you very much – SnC Apr 30 '13 at 6:48

Your Answer


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.