7
$\begingroup$
(*source*)
sourceStruct =
 {{a, {{c1, d}, {e, f}}}, {b, {{c2, h}, {i, k}}}, {m, {{c3, n}, {v,x}}}}
(*target*)
targetStruct = {{a, {{d}, {e, f}}}, {b, {{h}, {i,k}}}, {m, {{n}, {v, x}}}}
(*try 1*)
Apply[Function[x, Delete[x, 1]], Level[sourceStruct, {2}], {1}]
(*result 1*)
{a, {d}, b, {h}, m, {n}}
(*try 2*)
Map[Function[x, Delete[x, 1]], sourceStruct, {3}]
(*result 2*)
{{a, {{d}, {f}}}, {b, {{h}, {k}}}, {m, {{n}, {x}}}}
$\endgroup$
  • 1
    $\begingroup$ Some prose to explain what you intend is usually useful and will help you getting better answers by making answering easier than just having to parse some code and deducing the intentions thereof. $\endgroup$ – Yves Klett Nov 6 '14 at 10:49
11
$\begingroup$

Methods that require evaluation

You might use MapAt along with a function (kill) that will be removed when an expression evalutes:

_kill := Sequence[]

MapAt[kill, sourceStruct, {All, 2, 1, 1}]
{{a, {{d}, {e, f}}}, {b, {{h}, {i, k}}}, {m, {{n}, {v, x}}}}

This is particularly flexible because you can use multiple specifications, and it supports All and Span.

Similarly you could make an assignment with Part but the expression must evaluate before the Sequence[] expressions are removed:

sS2 = sourceStruct;                (* make a copy, else this will modify in-place*)
sS2[[All, 2, 1, 1]] = Sequence[];  (* Sequence[] still in memory *)

sS2
{{a, {{d}, {e, f}}}, {b, {{h}, {i, k}}}, {m, {{n}, {v, x}}}}

A terse option is Delete and Map:

Delete[#, {2, 1, 1}] & /@ sourceStruct
{{a, {{d}, {e, f}}}, {b, {{h}, {i, k}}}, {m, {{n}, {v, x}}}}

Perhaps closer to your intention, as it includes a levelspec, is Replace:

Replace[sourceStruct, {{_, a_}, b_} :> {{a}, b}, {2}]
{{a, {{d}, {e, f}}}, {b, {{h}, {i, k}}}, {m, {{n}, {v, x}}}}

A method that does not require evaluation

If evaluation is not possible then I recommend Delete alone which will require creating a list of positions:

pos = Array[{#, 2, 1, 1} &, Length@sourceStruct]

Delete[sourceStruct, pos]
{{1, 2, 1, 1}, {2, 2, 1, 1}, {3, 2, 1, 1}}

{{a, {{d}, {e, f}}}, {b, {{h}, {i, k}}}, {m, {{n}, {v, x}}}}
$\endgroup$
0
$\begingroup$

Not a general solution but consider the following approach if result2 is your objective:

 sourceStruct = {{a, {{c1, d}, {e, f}}}, {b, {{c2, h}, {i,k}}}, {m, {{c3, n}, {v, x}}}}

positions=Flatten[Map[Position[sourceStruct, #] &,{c1, e, c2, i, c3, v}], 1]
Delete[sourceStruct, positions]

{{a, {{d}, {f}}}, {b, {{h}, {k}}}, {m, {{n}, {x}}}}

A more general solution for result2, consider a variation of previous answers:

positions=Flatten[Table[{i,2,j,1},{i,1,Length[sourceStruct]},{j,1,Length[sourceStruct]-1}],1]   
Delete[sourceStruct, positions]

{{a, {{d}, {f}}}, {b, {{h}, {k}}}, {m, {{n}, {x}}}}

And, by extension, adding to the elegant solution by Wizard:

  _kill := Sequence[]
sourceStruct = {{a, {{c1, d}, {e, f}}}, {b, {{c2, h}, {i, k}}}, {m, {{c3, n}, {v, x}}}};
result2 = MapAt[kill, sourceStruct, {All, 2, All, 1}]

{{a, {{d}, {f}}}, {b, {{h}, {k}}}, {m, {{n}, {x}}}}
$\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.