Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #10556 > unrolled thread

altering each member of a list that matches a certain pattern

Started byJason Ebaugh <ebaughjason@gmail.com>
First post2012-06-20 07:54 +0000
Last post2012-06-22 07:03 +0000
Articles 7 — 7 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  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

#10556 — altering each member of a list that matches a certain pattern

FromJason Ebaugh <ebaughjason@gmail.com>
Date2012-06-20 07:54 +0000
Subjectaltering each member of a list that matches a certain pattern
Message-ID<jrrvjp$4sn$1@smc.vnet.net>
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

[toc] | [next] | [standalone]


#10562

FromRichard Fateman <fateman@cs.berkeley.edu>
Date2012-06-21 09:18 +0000
Message-ID<jruoti$fj9$1@smc.vnet.net>
In reply to#10556
On 6/20/2012 12:54 AM, Jason Ebaugh wrote:

>
> Am I missing a more straightforward way to do this?
Yes. Why not just...

Table[If[x > 9, SuperStar[x], x], {x, list}]

this does not affect the data in list.

OR


Map[Function[{x}, If[x > 9, SuperStar[x], x]], list]

which is, I expect, just as efficient and far more readable than your

If[# > 9, SuperStar[#], #] & /@ list

The non-alpha sequence [#]#]&/@   is hardly the worst puzzle
that can be constructed in 8 characters, but is, to me
unnecessarily obscure.

[toc] | [prev] | [next] | [standalone]


#10568

FromDavid Bailey <dave@removedbailey.co.uk>
Date2012-06-21 09:22 +0000
Message-ID<jrup48$fm3$1@smc.vnet.net>
In reply to#10556
On 20/06/2012 08:54, 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?
>
In[21]:= test = {19, 16, 5, 13, 10, 20, 1, 3, 18, 8, 9, 6, 12, 7, 4,
   15, 11, 14, 2, 17}

Out[21]= {19, 16, 5, 13, 10, 20, 1, 3, 18, 8, 9, 6, 12, 7, 4, 15, 11, \
14, 2, 17}

In[22]:= Map[If[# >= 10, SuperStar[#], #, #] &, test]

Out[22]= {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]}

David Bailey
http://www.dbaileyconsultancy.co.uk


[toc] | [prev] | [next] | [standalone]


#10575

From"Chris Degnen" <degnen@cwgsy.net>
Date2012-06-21 09:19 +0000
Message-ID<jruovf$fk4$1@smc.vnet.net>
In reply to#10556
> Am I missing a more straightforward way to do this?

list = RandomSample[Range[20], 20];
list /. x_ /; x > 9 :> SuperStar[x]


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
> 


[toc] | [prev] | [next] | [standalone]


#10581

FromDavid Reiss <dbreiss@gmail.com>
Date2012-06-21 09:26 +0000
Message-ID<jrupbt$fog$1@smc.vnet.net>
In reply to#10556
 {19, 16, 5, 13, 10, 20, 1, 3, 18, 8, 9, 6, 12, 7, 4, 15, 11, 14, 2,
  17} /. {z_?(# >= 10 &) :> SuperStar[z]}

--David


On Jun 20, 3:54 am, Jason Ebaugh <ebaughja...@gmail.com> 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 d=
o 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

[toc] | [prev] | [next] | [standalone]


#10583

FromVince Virgilio <blueschi@gmail.com>
Date2012-06-22 07:00 +0000
Message-ID<js156e$kde$1@smc.vnet.net>
In reply to#10556
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]

[toc] | [prev] | [next] | [standalone]


#10588

Fromawnl <awnl@gmx-topmail.de>
Date2012-06-22 07:03 +0000
Message-ID<js15c6$kfu$1@smc.vnet.net>
In reply to#10556
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web