Re: Replace list element based on a condition how to
- To: mathgroup at smc.vnet.net
- Subject: [mg106669] Re: [mg106630] Replace list element based on a condition how to
- From: "David Park" <djmpark at comcast.net>
- Date: Wed, 20 Jan 2010 06:51:33 -0500 (EST)
- References: <19147537.1263896562992.JavaMail.root@n11>
How about:
lst1 = {18, 19, 20, 21, 22, 23, 0, 1, 2, 3};
Attributes[fncHourFix] = Listable;
fncHourFix[h_] := If[(h - 6) < 0, h - 6 + 24, h - 6]
fncHourFix[lst1]
{12, 13, 14, 15, 16, 17, 18, 19, 20, 21}
If you didn't want to make fncHourFix Listable you could map it onto the
list with the same result.
fncHourFix /@ lst1
David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/
From: Canopus56 [mailto:canopus56 at yahoo.com]
Q1. Is there a way to replace a list based on a condition?
I have a list of integers -
lst1 = {18, 19, 20, 21, 22, 23, 0, 1, 2, 3}
that I would like to replace with the following rule -
fncHourFix[h_] := If[(h - 6) < 0, h - 6 + 24, h - 6];
but the replace function -
lst2 = lst1 /. {i_} :> fncHourFix[{i}]
- bombs out.
I've tried several variants.
Any pointers would be appreciated.
Thanks - Kurt