Re: First nonzero in list

• To: mathgroup at smc.vnet.net
• Subject: [mg110843] Re: First nonzero in list
• Date: Thu, 8 Jul 2010 06:49:58 -0400 (EDT)

```A possible solution

In[45]:= th /. {pre : ___, post : (0) ..} :> {pre}

Out[45]= {227, 342, 527, 0, 670, 730, 703, 0, 0, 649, 687, 614, 570, 501,
462, 402, \
325, 254, 189, 99, 81, 36, 9, 8, 3}

In[46]:= Length[th /. {pre : ___, post : (0) ..} :> {pre}]
Out[46]= 25

2010/7/8 S. B. Gray <stevebg at roadrunner.com>

> I have lists such as
>
> th = {227, 342, 527, 0, 670, 730, 703, 0, 0, 649, 687, 614, 570, 501,
>   462, 402, 325, 254, 189, 99, 81, 36, 9, 8, 3, 0, 0, 0, 0, 0, 0, 0,
>   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
>    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
>
> and I want to find the position of the last nonzero element (e.g. 3 at
> position 25). Trivial, but some poking around did not reveal a neat way.
>
> Anyone? Thank you.
>
> Steve Gray
>
>

```

• Prev by Date: Re: First nonzero in list
• Next by Date: Re: Pattern: x_List with conditions on elements
• Previous by thread: change \$UserBaseDirectory ?
• Next by thread: Re: First nonzero in list