3
$\begingroup$

I have a list like this for exmple:

{{{1,2},{3,4}},{{1,2}},{{1,2},{3,4},{5,6}}}

So now i have to copy the form of the list with oen depth less so my result would be :

{{0,0},{0},{0,0,0}}

The first list might be a millions of points long so it should be kinda time efficient. Any Ideas would be great, Thanks in advance

$\endgroup$
3
  • 4
    $\begingroup$ While you wait for a fast answer have a play around with {{{1, 2}, {3, 4}}, {{1, 2}}, {{1, 2}, {3, 4}, {5, 6}}} /. {__Integer} -> 0 $\endgroup$ Dec 3, 2019 at 19:44
  • 2
    $\begingroup$ Will it always be a 3D list where each element is a list of pairs? Or are there other possibilities? Replace[yourlist, {_, _} -> 0, {2}] yields the correct result, but it may not cover all the possible kinds of lists you need to deal with. $\endgroup$
    – MassDefect
    Dec 3, 2019 at 19:50
  • $\begingroup$ Yeah, it will always be 3D $\endgroup$ Dec 3, 2019 at 20:03

2 Answers 2

4
$\begingroup$
  list = {{{1, 2}, {3, 4}}, {{1, 2}}, {{1, 2}, {3, 4}, {5, 6}}};

  Total[list 0, {3}]

Or

Apply[Total, list 0, {2}]

Or

ConstantArray[0, #] & /@ Length /@ list

{{0, 0}, {0}, {0, 0, 0}}

$\endgroup$
3
$\begingroup$
Map[0 &, list, {2}]

{{0, 0}, {0}, {0, 0, 0}}

Also

list /. _?VectorQ -> 0

{{0, 0}, {0}, {0, 0, 0}}

$\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.