Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #15137 > unrolled thread

Re: Define function using lists or tables

Started byAlexei Boulbitch <Alexei.Boulbitch@iee.lu>
First post2013-07-06 08:59 +0000
Last post2013-07-06 08:59 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Re: Define function using lists or tables Alexei Boulbitch <Alexei.Boulbitch@iee.lu> - 2013-07-06 08:59 +0000

#15137 — Re: Define function using lists or tables

FromAlexei Boulbitch <Alexei.Boulbitch@iee.lu>
Date2013-07-06 08:59 +0000
SubjectRe: Define function using lists or tables
Message-ID<kr8m8t$ds$1@smc.vnet.net>
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]]

8

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

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

p

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.
IEE S.A.
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@iee.lu




[toc] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web