MathGroup Archive 2013

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Define function using lists or tables

Hello. Possibly this is a very simple question.

Is there a way to define a function over a (BIG) list of ordered pairs? I mean, suppose that I have a list, created using a Table command or something like that, of the form [{a,b},{c,d},etc.] such that, after naming it f, say, f = [{a,b},{c,d},etc.], then, afterwards, whenever there is a command such that one needs to compute f[a] (or f[c]), the program immediately replaces f[a] by b,and f[c] by d?

Thank you.

Hi, Joaquim,

One way may be like the following. This is a list to play with:

lst = Partition[Map[ToExpression, CharacterRange["a", "z"]], 2]

{{a, b}, {c, d}, {e, f}, {g, h}, {i, j}, {k, l}, {m, n}, {o, p}, {q,
  r}, {s, t}, {u, v}, {w, x}, {y, z}}

Let us take, for example, o and try with it. Its position is

Position[lst, o]

{{8, 1}}

We only need the position of the corresponding pair:

Position[lst, o][[1, 1]]


Then the second pair member, p, has the coordinate [[8,2]]:

lst[[Position[lst, o][[1, 1]], 2]]


Now we can write a function doing all this:

f[lst_, x_] := lst[[Position[lst, x][[1, 1]], 2]]

 Let us try it:

Map[f[lst, #] &, {a, e, i, w}]

{b, f, j, x}

Have fun, Alexei

Alexei BOULBITCH, Dr., habil.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG

Office phone :  +352-2454-2566
Office fax:       +352-2454-3566
mobile phone:  +49 151 52 40 66 44

e-mail: alexei.boulbitch at

  • Prev by Date: Re: Re: Animate Command and Vector Reference
  • Next by Date: Re: Re: Animate Command and Vector Reference
  • Previous by thread: Re: Define function using lists or tables
  • Next by thread: Re: Rather simple function returns curious result. Explanation