1
$\begingroup$

consider a list as follows:

list = {{{{{{{1, 1}, {1, 1}}, 0.}, {{{1, 1}, {1, 2}}, 
  29.7586}}, {{{{1, 1}, {2, 1}}, 0.165637}, {{{1, 1}, {2, 2}}, 
  37.2042}}}, {{{{{1, 2}, {1, 1}}, 29.7586}, {{{1, 2}, {1, 2}}, 
  0.}}, {{{{1, 2}, {2, 1}}, 29.7492}, {{{1, 2}, {2, 2}}, 
  32.2175}}}}, {{{{{{2, 1}, {1, 1}}, 0.165637}, {{{2, 1}, {1, 2}},
   29.7492}}, {{{{2, 1}, {2, 1}}, 0.}, {{{2, 1}, {2, 2}}, 
  37.2055}}}, {{{{{2, 2}, {1, 1}}, 37.2042}, {{{2, 2}, {1, 2}}, 
  32.2175}}, {{{{2, 2}, {2, 1}}, 37.2055}, {{{2, 2}, {2, 2}}, 
  0.}}}}};

I want to delete the elements {{{1, 1}, {1, 1}}, 0.}, {{{1, 2}, {1, 2}}, 0.},{{{2, 1}, {2, 1}}, 0.} and {{{2, 2}, {2, 2}}, 0.}

How can I do this?

Edit 1

Let me breakdown the problem. Consider,

val = {{0., 29.7586, 0.165637, 37.2042}, {29.7586, 0., 29.7492, 
    32.2175}, {0.165637, 29.7492, 0., 37.2055}, {37.2042, 32.2175, 
    37.2055, 0.}};
coordpair = 
  Table[{{i, j}, {k, l}}, {i, 1, 2, 1}, {j, 1, 2, 1}, {k, 1, 2, 
    1}, {l, 1, 2, 1}];

I have constructed the list by combining val and coordpair. Is it possible to delete the cases in coordpair where {i, j} == {k, l}?

$\endgroup$
1
  • $\begingroup$ perhaps Delete[list, Position[list, Alternatives @@ del]] would be relevant? (del is a list of the items to delete.) $\endgroup$
    – user42582
    Commented Jan 24, 2018 at 10:34

3 Answers 3

3
$\begingroup$
DeleteCases[list, {{{i_, j_}, {i_, j_}}, _}, Infinity] 
Pick[list, Map[Unequal @@ First[#] &, list, {-4}]] 
Pick[list, Map[#[[1, 1]] =!= #[[1, 2]] &, list, {-4}]] 
Delete[list, Position[list, {{a_, a_}, _}, {4}]]

all give

{{{{{{{1, 1}, {1, 2}}, 29.7586}}, {{{{1, 1}, {2, 1}}, 0.165637},
{{{1, 1}, {2, 2}}, 37.2042}}}, {{{{{1, 2}, {1, 1}}, 29.7586}},
{{{{1, 2}, {2, 1}}, 29.7492}, {{{1, 2}, {2, 2}}, 32.2175}}}},
{{{{{{2, 1}, {1, 1}}, 0.165637}, {{{2, 1}, {1, 2}}, 29.7492}},
{{{{2, 1}, {2, 2}}, 37.2055}}}, {{{{{2, 2}, {1, 1}}, 37.2042},
{{{2, 2}, {1, 2}}, 32.2175}}, {{{{2, 2}, {2, 1}}, 37.2055}}}}}

And

to delete the cases in coordpair where {i, j} == {k, l}

DeleteCases[coordpair, {{i_, j_}, {i_, j_}}, Infinity] 
Pick[coordpair, Map[Unequal @@ # &, coordpair, {-3}]]  
Pick[coordpair, Map[#[[1]] =!= #[[2]] &, coordpair, {-3}]] 
Delete[coordpair, Position[coordpair, {a_, a_}, {4}]]

{{{{{{1, 1}, {1, 2}}}, {{{1, 1}, {2, 1}}, {{1, 1}, {2, 2}}}},
{{{{1, 2}, {1, 1}}}, {{{1, 2}, {2, 1}}, {{1, 2}, {2, 2}}}}},
{{{{{2, 1}, {1, 1}}, {{2, 1}, {1, 2}}}, {{{2, 1}, {2, 2}}}},
{{{{2, 2}, {1, 1}}, {{2, 2}, {1, 2}}}, {{{2, 2}, {2, 1}}}}}}

$\endgroup$
1
  • $\begingroup$ This is fine for a short list. But for a larger list, it will be a problem. I am updating my question for a better understanding. $\endgroup$
    – user36426
    Commented Jan 24, 2018 at 11:27
2
$\begingroup$
list = {{{{{{{1, 1}, {1, 1}}, 0.}, {{{1, 1}, {1, 2}}, 
       29.7586}}, {{{{1, 1}, {2, 1}}, 0.165637}, {{{1, 1}, {2, 2}}, 
       37.2042}}}, {{{{{1, 2}, {1, 1}}, 29.7586}, {{{1, 2}, {1, 2}}, 
       0.}}, {{{{1, 2}, {2, 1}}, 29.7492}, {{{1, 2}, {2, 2}}, 
       32.2175}}}}, {{{{{{2, 1}, {1, 1}}, 
       0.165637}, {{{2, 1}, {1, 2}}, 29.7492}}, {{{{2, 1}, {2, 1}}, 
       0.}, {{{2, 1}, {2, 2}}, 37.2055}}}, {{{{{2, 2}, {1, 1}}, 
       37.2042}, {{{2, 2}, {1, 2}}, 32.2175}}, {{{{2, 2}, {2, 1}}, 
       37.2055}, {{{2, 2}, {2, 2}}, 0.}}}}};

list2 = DeleteCases[list, _?(#[[1, 1]] == #[[1, 2]] &), {4}]

(* {{{{{{{1, 1}, {1, 2}}, 29.7586}}, {{{{1, 1}, {2, 1}}, 
     0.165637}, {{{1, 1}, {2, 2}}, 37.2042}}}, {{{{{1, 2}, {1, 1}}, 
     29.7586}}, {{{{1, 2}, {2, 1}}, 29.7492}, {{{1, 2}, {2, 2}}, 
     32.2175}}}}, {{{{{{2, 1}, {1, 1}}, 0.165637}, {{{2, 1}, {1, 2}}, 
     29.7492}}, {{{{2, 1}, {2, 2}}, 37.2055}}}, {{{{{2, 2}, {1, 1}}, 
     37.2042}, {{{2, 2}, {1, 2}}, 32.2175}}, {{{{2, 2}, {2, 1}}, 
     37.2055}}}}} *)

Complement[Flatten[list, 3], Flatten[list2, 3]]

(* {{{{1, 1}, {1, 1}}, 0.}, {{{1, 2}, {1, 2}}, 0.}, {{{2, 1}, {2, 1}}, 
  0.}, {{{2, 2}, {2, 2}}, 0.}} *)
$\endgroup$
1
$\begingroup$

Assuming you constructed it similarly to this

list = Join[
        ArrayReshape[coordpair, Insert[Dimensions[coordpair], 1, 5]],
        ArrayReshape[val, Append[Dimensions[coordpair][[;; 4]], 1]], 5]

Then the deletion is:

MapIndexed[If[#2[[1]] =!= #2[[3]] || #2[[2]] =!= #2[[4]], #, Nothing] &, list, {4}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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