Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #10583
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!69.16.185.11.MISMATCH!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!nx02.iad01.newshosting.com!newshosting.com!newspump.sol.net!posts.news.twtelecom.net!nnrp2.twtelecom.net!not-for-mail |
|---|---|
| From | Vince Virgilio <blueschi@gmail.com> |
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: altering each member of a list that matches a certain pattern |
| Date | Fri, 22 Jun 2012 07:00:30 +0000 (UTC) |
| Organization | Steven M. Christensen and Associates, Inc and MathTensor, Inc. |
| Sender | steve@smc.vnet.net |
| Approved | Steven M. Christensen <steve@smc.vnet.net>, Moderator |
| Message-ID | <js156e$kde$1@smc.vnet.net> (permalink) |
| References | <jrrvjp$4sn$1@smc.vnet.net> |
| Lines | 55 |
| NNTP-Posting-Date | 22 Jun 2012 07:04:56 GMT |
| NNTP-Posting-Host | 4742fe60.news.twtelecom.net |
| X-Trace | DXC=ham:L<o\Cb;>Y`AN6T`Vj1C_A=>8kQj6=;[h;PUXBgb4VXYV4MiX_A?EFiONJ7[Go62\oB^ONN1m2 |
| X-Complaints-To | abuse@twtelecom.net |
| X-Received-Bytes | 2447 |
| Xref | csiph.com comp.soft-sys.math.mathematica:10583 |
Show key headers only | View raw
On Wednesday, June 20, 2012 3:54:33 AM UTC-4, Jason Ebaugh wrote:
> Good people,
>
>
> I need to alter each member of a list that matches a certain pattern.
> However, I need the whole list back, in its original order.
> So Cases[] on its own doesn't do the trick.
>
> Here is a toy example that is analogous to my real problem:
> Put an asterisk on each value that is 10 or greater in the following list.
>
> list = RandomSample[Range[20], 20]
>
> Out[1]= {19, 16, 5, 13, 10, 20, 1, 3, 18, 8, 9, 6, 12, 7, 4, 15, 11, 14, 2, 17}
>
> I came up with two ways to do this.
> However, I feel like I may be missing a simpler, single function way to do this.
>
> Method 1:
> If[# > 9, SuperStar[#], #] & /@ list
>
> Out[2]= {SuperStar[19], SuperStar[16], 5, SuperStar[13], SuperStar[10],
> SuperStar[20], 1, 3, SuperStar[18], 8, 9, 6, SuperStar[12], 7, 4,
> SuperStar[15], SuperStar[11], SuperStar[14], 2, SuperStar[17]}
>
>
> Method 2:
> ReplacePart[list,
> Table[{Position[list, x_ /; x > 9][[i, 1]] ->
> Map[SuperStar, Cases[list, x_ /; x > 9]][[i]]}, {i,
> Length[Cases[list, x_ /; x > 9]]}] // Flatten]
>
> Out[3]= {SuperStar[19], SuperStar[16], 5, SuperStar[13], SuperStar[10],
> SuperStar[20], 1, 3, SuperStar[18], 8, 9, 6, SuperStar[12], 7, 4,
> SuperStar[15], SuperStar[11], SuperStar[14], 2, SuperStar[17]}
>
>
> Am I missing a more straightforward way to do this?
>
>
>
> Thanks,
> Jason
>
>
> Jason Ebaugh
There is also MapAt:
rs = RandomSample[Range[20], 20];
pos = Position[rs, _?(# > 9&)];
MapAt[SuperStar, rs, pos]
Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
altering each member of a list that matches a certain pattern Jason Ebaugh <ebaughjason@gmail.com> - 2012-06-20 07:54 +0000 Re: altering each member of a list that matches a certain pattern Richard Fateman <fateman@cs.berkeley.edu> - 2012-06-21 09:18 +0000 Re: altering each member of a list that matches a certain pattern David Bailey <dave@removedbailey.co.uk> - 2012-06-21 09:22 +0000 Re: altering each member of a list that matches a certain pattern "Chris Degnen" <degnen@cwgsy.net> - 2012-06-21 09:19 +0000 Re: altering each member of a list that matches a certain pattern David Reiss <dbreiss@gmail.com> - 2012-06-21 09:26 +0000 Re: altering each member of a list that matches a certain pattern Vince Virgilio <blueschi@gmail.com> - 2012-06-22 07:00 +0000 Re: altering each member of a list that matches a certain pattern awnl <awnl@gmx-topmail.de> - 2012-06-22 07:03 +0000
csiph-web