3
$\begingroup$

I have a list of the form list={list1,list2,...}, where list1 is of the form {a,b,c,d} and similarly for other sublists, i.e. list2,list3 etc.

I would like to select the sublists of the list that have $a\neq b$ and $c\neq d$. I tried to use DeleteCases, but I am not sure how to do it properly.

$\endgroup$
1
  • 1
    $\begingroup$ Select will be more natural here. $\endgroup$
    – Kuba
    Jan 5 '18 at 13:31
4
$\begingroup$
SeedRandom[123]
data = RandomChoice[CharacterRange["a", "d"], {10, 4}]

{{"b", "d", "b", "a"}, {"c", "c", "c", "c"}, {"a", "a", "a", "c"},
{"b", "c", "b", "d"}, {"c", "b", "c", "a"}, {"a", "d", "c", "b"},
{"c", "a", "d", "d"}, {"c", "c", "b", "b"}, {"a", "c", "d", "a"}, {"d", "a", "c", "c"}}

DeleteCases[data, {a_, a_, _, _} | {_, _, b_, b_}]

{{"b", "d", "b", "a"}, {"b", "c", "b", "d"}, {"c", "b", "c", "a"}, {"a", "d", "c", "b"}, {"a", "c", "d", "a"}}

Select[data, UnsameQ @@ #[[;; 2]] && UnsameQ @@ #[[3 ;;]] &]

{{"b", "d", "b", "a"}, {"b", "c", "b", "d"}, {"c", "b", "c", "a"}, {"a", "d", "c", "b"}, {"a", "c", "d", "a"}}

$\endgroup$
4
$\begingroup$

Another way:

Delete[data, Position[
    Differences[ArrayReshape[data, {Length[data], 2, 2}], {0, 1}],
    {{0, 0}}]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.