Re: Nested List Abs[]
- To: mathgroup at smc.vnet.net
- Subject: [mg121044] Re: Nested List Abs[]
- From: Ray Koopman <koopman at sfu.ca>
- Date: Wed, 24 Aug 2011 07:50:59 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <j328jc$b4n$1@smc.vnet.net>
On Aug 24, 12:18 am, Sam Korn <s... at kornfamily.com> wrote:
> I have a list, and each element of the
> list is a sublist with two integers. How do I delete from the larger
> list any integer pair whose numerical distance apart is greater than
> one? I'd been trying to map Abs[Subtract[#1,#2]]&<1 over the list, but
> so far that hasn't worked.
>
> Any suggestions?
>
> Sent from my iPhone
Here are five ways to do it. There are many more.
Cases[data, {x_,y_} /; Abs[x-y] < 2 ]
Select[data, Abs[Subtract @@ # ] < 2 &]
Pick[data, Subtract @@ Transpose @ data, -1|0|1 ]
Pick[data, Thread[ Abs[ Subtract @@ Transpose @ data] < 2 ] ]
Pick[data, UnitStep[ Abs[ Subtract @@ Transpose @ data ] - 2 ], 0 ]