Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1996
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1996

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How can this MapAt application be done more efficiently

  • To: mathgroup at smc.vnet.net
  • Subject: [mg4206] Re: How can this MapAt application be done more efficiently
  • From: espen.haslund at fys.uio.no (Espen Haslund)
  • Date: Thu, 13 Jun 1996 23:11:44 -0400
  • Organization: Universitet i Oslo
  • Sender: owner-wri-mathgroup at wolfram.com

In article <4pis80$iuo at dragonfly.wolfram.com>, cannon at alpha.centenary.edu 
says...
>
>I have written a function to Drop any zero's from a table of numbers.
>Here is an example:
>
>        In[161]:= dropzero[ {{5, 0}, {4, 1}, {3, 2, 0}, {3, 1, 1}} ]
>
>        Out[161]= {{5}, {4, 1}, {3, 2}, {3, 1, 1}}
>
>I would like to learn how to do it more efficiently. In what I am
>doing, the 0's will always occur as the last element of the inner
>lists (although it would be nice to have it work in general).
>
Hi Joel:

Here is one alternative using Select. I am not sure 
about the efficiency, but it is shorter:

In:  dropzero[lis_] := Map[Select[#, (# =!= 0)&] &, lis]


In:  dropzero[{{5, 0}, {4, 1}, {3, 2, 0}, {3, 1, 1}}]

Out: {{5}, {4, 1}, {3, 2}, {3, 1, 1}}


This should work for your case. It dont need the zeroes 
to appear last, but they should appear in 1. level sublists.

Note the "nested" use of pure functions: The two #'s act at
different levels.

For a more general case (including also your case) the following
may perhaps work:

In:  dropzeroAllLevels[lis_] :=
      Map[If[ListQ[#], Select[#, (# =!= 0)&], #] &, 
       Select[lis, (# =!= 0)&], Infinity]


In:  dropzeroAllLevels[{0,{5, 0}, {4, 1}, {3, 2, 0}, {3, 1, {1,0,1}}}]

Out: {{5}, {4, 1}, {3, 2}, {3, 1, {1, 1}}}


Hope this is of some help 

-Espen



==== [MESSAGE SEPARATOR] ====


  • Prev by Date: Running Mathematica 2.1 under Windows95?
  • Next by Date: Re: How can this MapAt application be done more efficiently
  • Previous by thread: How can this MapAt application be done more efficiently
  • Next by thread: Re: How can this MapAt application be done more efficiently