Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #15137
| From | Alexei Boulbitch <Alexei.Boulbitch@iee.lu> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: Define function using lists or tables |
| Date | 2013-07-06 08:59 +0000 |
| Message-ID | <kr8m8t$ds$1@smc.vnet.net> (permalink) |
| Organization | Time-Warner Telecom |
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
Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread
Re: Define function using lists or tables Alexei Boulbitch <Alexei.Boulbitch@iee.lu> - 2013-07-06 08:59 +0000
csiph-web