Re: Replace
- To: mathgroup at smc.vnet.net
- Subject: [mg121103] Re: Replace
- From: Armand Tamzarian <mike.honeychurch at gmail.com>
- Date: Sat, 27 Aug 2011 08:18:13 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <j35am1$og8$1@smc.vnet.net> <j37p96$4lk$1@smc.vnet.net>
On Aug 26, 7:33 pm, Armand Tamzarian <mike.honeychu... at gmail.com> wrote: > On Aug 25, 9:12 pm, Sam Korn <s... at 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 > > Here are a couple of ways: > > tmp = RandomInteger[{1, 20}, {20, 2}] > > First using Replace > > tmp /. {x_, y_} :> x/y > > For many this is a more intuitive way of doing it but will be > (relatively) slow for very large lists. This is a faster way of doing > it: > > tmp[[All, 1]]/tmp[[All, 2]] > > Mike Doh! you wanted "|" not "/" It still looks like using Part will give the fastest solution (on my system at least, 8.0.1 OS X 10.6.8) tmp = RandomInteger[{1, 20}, {20000, 2}]; Timing[tmp /. {x_, y_} :> x | y;] Timing[Alternatives @@@ tmp;] Timing[Thread[tmp[[All, 1]] | tmp[[All, 2]]];] Mike