       Re: Or in a Select question

• To: mathgroup at smc.vnet.net
• Subject: [mg67123] Re: [mg67066] Or in a Select question
• From: bsyehuda at gmail.com
• Date: Fri, 9 Jun 2006 01:10:34 -0400 (EDT)
• References: <200606080854.EAA12393@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Hi Janos,
There is a logical error in your function.  use And in place of Or
since the Select function needs to select the pairs with both elements
different.
So
Select[pp, (#1[[1, 1]] =!= #1[[1, 2]] && #1[[2, 1]] =!= #1[[2, 2]])&]

I would like to suggest the pattern matching approch which is more intuitive
in this case
lst = {a, b, c};
pp = Partition[Tuples[lst, 2], 2, 1];
DeleteCases[pp, {{x_, x_}, _} | {_, {x_, x_}}]

regards
yehuda

On 6/8/06, J=E1nos <janos.lobb at yale.edu> wrote:
>
> Hi,
>
> I have a list
>
> lst={a,b,c}
>
> I make another list from it the following way:
>
> In:=
> pp = Partition[Tuples[lst,
>      2], 2, 1]
> Out=
> {{{a, a}, {a, b}},
>    {{a, b}, {a, c}},
>    {{a, c}, {b, a}},
>    {{b, a}, {b, b}},
>    {{b, b}, {b, c}},
>    {{b, c}, {c, a}},
>    {{c, a}, {c, b}},
>    {{c, b}, {c, c}}}
>
> From here I would like to select all the elements whose sublists
> contain only different elements.  So my "logical" selection was:
>
> In:=
> Select[pp,
>    #1[[1,1]] =!= #1[[1,2]] ||
>      #1[[2,1]] =!= #1[[2,
>        2]] & ]
> Out=
> {{{a, a}, {a, b}},
>    {{a, b}, {a, c}},
>    {{a, c}, {b, a}},
>    {{b, a}, {b, b}},
>    {{b, b}, {b, c}},
>    {{b, c}, {c, a}},
>    {{c, a}, {c, b}},
>    {{c, b}, {c, c}}}
>
> Well, that did not do any damage to the list.  After some time I came
> up with this one:
>
> In:=
> Complement[pp, Select[pp,
>     Xor[#1[[1,1]] =!=
>        #1[[1,2]],
>       #1[[2,1]] =!= #1[[2,
>         2]]] & ]]
> Out=
> {{{a, b}, {a, c}},
>    {{a, c}, {b, a}},
>    {{b, c}, {c, a}},
>    {{c, a}, {c, b}}}
>
> That looks OK, but also looks too complicated.  Why my "logical" one
> does not work here?
>
> Interestingly if I just use either the left or right side of the Or,
> that partial select is working.  For example:
>
> In:=
> Select[pp, #1[[1,1]] =!=
>      #1[[1,2]] & ]
> Out=
> {{{a, b}, {a, c}},
>    {{a, c}, {b, a}},
>    {{b, a}, {b, b}},
>    {{b, c}, {c, a}},
>    {{c, a}, {c, b}},
>    {{c, b}, {c, c}}}
>
> Now if I try with Cases and conditional pattern matching then the
> selection for sublists with identical elements works:
>
> In:=
> Cases[pp, {u_, v_} /;
>     u[] === u[] ||
>      v[] === v[]]
> Out=
> {{{a, a}, {a, b}},
>    {{b, a}, {b, b}},
>    {{b, b}, {b, c}},
>    {{c, b}, {c, c}}}
>
> If I change here the === to =!=, then I do not get again that I expect:
>
> In:=
> Cases[pp,
>    (({u_, v_} /; u[]) =!=
>       u[] || v[]) =!=
>     v[]]
> From In:=
> \!\(\*
>    RowBox[{\(Part::"partd"\),
>      ":", "\<\"Part specification \\!\\(u \[LeftDoubleBracket] 2 \
> \[RightDoubleBracket]\\) is longer than depth of object. \
> \\!\\(\\*ButtonBox[\\\"More\[Ellipsis]\\\", ButtonStyle->\\
> \\\", ButtonFrame->None, ButtonData:>\\\"General::partd\\\"]\\)\"\>"}]\)
> From In:=
> \!\(\*
>    RowBox[{\(Part::"partd"\),
>      ":", "\<\"Part specification \\!\\(v \[LeftDoubleBracket] 2 \
> \[RightDoubleBracket]\\) is longer than depth of object. \
> \\!\\(\\*ButtonBox[\\\"More\[Ellipsis]\\\", ButtonStyle->\\
> \\\", ButtonFrame->None, ButtonData:>\\\"General::partd\\\"]\\)\"\>"}]\)
> Out=
> {}
> From In:=
> Part::"partd":"Part specification \!\(u \[LeftDoubleBracket] 2 \
> \[RightDoubleBracket]\) is longer than depth of object. \
> \", \
> ButtonFrame->None, ButtonData:>\"General::partd\"]\)"
> From In:=
> Part::"partd":"Part specification \!\(v \[LeftDoubleBracket] 2 \
> \[RightDoubleBracket]\) is longer than depth of object. \
> \", \
> ButtonFrame->None, ButtonData:>\"General::partd\"]\)"
>
> if I change =!= to only != then I still do not get that I expect:
>
> In:=
> Cases[pp, {u_, v_} /;
>     u[] != u[] ||
>      v[] != v[]]
> Out=
> {}
>
> Obviously I am not GETting something here :)
>
>
> J=E1nos
> P.S.  It is 5.1 on OSX 10.4.6.  I know that Or evaluates in a non-
> traditional way and looked the Appendix - that is how I ended up with
> Xor.
>
>
>
>
>
>
> ----------------------------------------------
> Trying to argue with a politician is like lifting up the head of a
> corpse.
> (S. Lem: His Master Voice)
>
>

```

• Prev by Date: Re: Or in a Select question
• Next by Date: rules for using functions as basis vectors
• Previous by thread: Re: Or in a Select question
• Next by thread: Re: Or in a Select question