Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: altering each member of a list that matches a certain pattern

  • To: mathgroup at smc.vnet.net
  • Subject: [mg126990] Re: altering each member of a list that matches a certain pattern
  • From: awnl <awnl at gmx-topmail.de>
  • Date: Fri, 22 Jun 2012 03:48:13 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <jrrvjp$4sn$1@smc.vnet.net>

Hi,

> 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.

You have received a lot of answers which suggested to use /. (shortcut 
for ReplaceAll). While this will do what you expect for the given 
example I consider it to be one of the most common sources of errors 
when used for a programmatic task, which I understand your question is 
about. ReplaceAll will replace at all levels and that is not what you 
really want in most cases. Replacing subexpressions within mathematical 
expression is the big exception and most probably the reason why 
ReplaceAll is used so frequently in Mathematica and received an own 
shortcut.

If you want replace "each member of a list" and expect a similar 
behavior as Cases, you should probably rather use Replace with a level 
specification, like this:

Replace[list,x_/;x>9 :> SuperStar[x],{1}]

I think your solution 1 which maps onto the list will be more efficient 
and there isn't much difference concerning readability, so in this case 
I wouldn't consider Replace to be a better solution really...

hth,

albert



  • Prev by Date: Re: Default path for opening files
  • Next by Date: Comparison of Contour Plots between Various Programs
  • Previous by thread: Re: altering each member of a list that matches a certain pattern
  • Next by thread: Mathematica on tablet computer