Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #4586
| From | "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: Replace |
| Date | 2011-08-26 09:30 +0000 |
| Organization | Steven M. Christensen and Associates, Inc and MathTensor, Inc. |
| Message-ID | <j37p4d$4jv$1@smc.vnet.net> (permalink) |
| References | <j35am1$og8$1@smc.vnet.net> |
On Thu, 25 Aug 2011 12:12:01 +0100, Sam Korn <sam@kornfamily.com> wrote:
> Hi, another stupid question. I have a list, and each element in the list
> is
> a sublist with a pair of integers. How do I replace each element along
> the
> lines of "{5,6}" with "(5|6)"?
>
> I know it's a stupid question, and there's probably something really
> obvious
> I'm missing, but I'm really new to Mathematica.
>
> Thanks!
> -Sam
In[1] :=
list = RandomInteger[{-5, 5}, {10, 2}]
Out[1] =
{{-4, -4}, {-1, -3}, {4, 4}, {0, -4}, {-5, 3}, {-5, 4}, {-1, -2}, {5, -2},
{1, -1}, {-4, 1}}
In[2] :=
list /. {x_, y_} :> (x | y)
Out[2] =
{-4 | -4, -1 | -3, 4 | 4, 0 | -4, -5 | 3, -5 | 4, -1 | -2, 5 | -2, 1 | -1,
-4 | 1}
("/." is a sigil used in infix notation to denote the function ReplaceAll.
":>" is RuleDelayed in the same sense.)
It's unclear to me if the above is what you intended; (x | y) is
understood by Mathematica as a pattern which matches either x or y. If you
had wanted something that simply displays as such without having any
particular meaning you may instead use
In[3] :=
list /. {x_, y_} :> StringForm["(`1`|`2`)", x, y]
Out[3] =
{(-4|-4),(-1|-3),(4|4),(0|-4),(-5|3),(-5|4),(-1|-2),(5|-2),(1|-1),(-4|1)}
or perhaps
In[4] :=
Format@customizedElement[x_, y_] := StringForm["(`1`|`2`)", x, y];
customizedElement @@@ list
Out[4] =
{(-4|-4),(-1|-3),(4|4),(0|-4),(-5|3),(-5|4),(-1|-2),(5|-2),(1|-1),(-4|1)}
which has the advantage of allowing other operations to be defined on
customizedElement if desired.
Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Replace Sam Korn <sam@kornfamily.com> - 2011-08-25 11:12 +0000
Re: Replace Ray Koopman <koopman@sfu.ca> - 2011-08-26 09:27 +0000
Re: Replace "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-08-26 09:30 +0000
Re: Replace Peter Breitfeld <phbrf@t-online.de> - 2011-08-26 09:31 +0000
Re: Replace Armand Tamzarian <mike.honeychurch@gmail.com> - 2011-08-26 09:33 +0000
Re: Replace Armand Tamzarian <mike.honeychurch@gmail.com> - 2011-08-27 12:22 +0000
Re: Replace Peter Pein <petsie@dordos.net> - 2011-08-28 08:09 +0000
Re: Replace Armand Tamzarian <mike.honeychurch@gmail.com> - 2011-08-29 07:49 +0000
Re: Replace Peter Pein <petsie@dordos.net> - 2011-08-26 09:35 +0000
Re: Replace Murray Eisenberg <murray@math.umass.edu> - 2011-08-27 12:25 +0000
Re: Replace Ray Koopman <koopman@sfu.ca> - 2011-08-28 08:06 +0000
Re: Replace Murray Eisenberg <murray@math.umass.edu> - 2011-08-28 08:13 +0000
csiph-web