Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #10580
| From | Adriano Pascoletti <adriano.pascoletti@uniud.it> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: altering each member of a list that matches a certain pattern |
| Date | 2012-06-21 09:25 +0000 |
| Organization | Steven M. Christensen and Associates, Inc and MathTensor, Inc. |
| Message-ID | <jrupav$fod$1@smc.vnet.net> (permalink) |
| References | <201206200751.DAA04994@smc.vnet.net> |
In[18]:= list /. {i_ /; i >= 10 :> SuperStar[i]}
Out[18]= {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]}
Adriano Pascoletti
2012/6/20 Jason Ebaugh <ebaughjason@gmail.com>
> 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
>
>
Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread
Re: altering each member of a list that matches a certain pattern Adriano Pascoletti <adriano.pascoletti@uniud.it> - 2012-06-21 09:25 +0000
csiph-web