Re: Re: Replace non-numeric values in a list
- To: mathgroup at smc.vnet.net
- Subject: [mg88806] Re: [mg88779] Re: Replace non-numeric values in a list
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Sat, 17 May 2008 05:29:31 -0400 (EDT)
- References: <g0h825$mtr$1@smc.vnet.net> <200805160933.FAA01118@smc.vnet.net>
On 16 May 2008, at 18:33, Steven Siew wrote: > On May 15, 9:50 pm, "Adel Elsabbagh" <aelsabb... at gmail.com> wrote: >> Dear all, >> >> I have a list that contains numeric and non-numeric values >> list = { a, 1, 2, b, 3}; >> How can I replace non-numeric values with zeros? >> Thanks! >> >> -- >> Adel Elsabbaghhttp://www.svlab-asu.com/aelsabbagh.html > > Surprisingly the script below fails. Anyone want to comment? > > list = { a, 1, 2, b, 3}; > > newlist2 = list /. v_?(Not[NumericQ[#]] &) -> 0 > > Not really surprising. "list" itself is not numeric so it matches the pattern. Search for pattern in ReplaceAll beigns at the bottom level and stops if a match is found so list is the first and only match. One way to avoid this is: newlist2 = Replace[list, v_?(Not[NumericQ[#]] &) -> 0, {1}] {0, 1, 2, 0, 3} Andrzej Kozlowski
- References:
- Re: Replace non-numeric values in a list
- From: Steven Siew <stevensiew2@gmail.com>
- Re: Replace non-numeric values in a list