From: Albert Retey on 13 Jun 2010 04:10 Am 12.06.2010 11:32, schrieb Rui: > I have always done very ugly code when trying to do something > seemingly simple and basic such as applying a function to subparts of a list. > Simple example, I want to divide by 2 all the second elements of a > list > {{a, 5}, {b, 6}, ...} --> {{a, 5/2}, ...} > > My ideas: > Transpose -> MapAt/Map -> Transpose > Build the sublist with Part and then rebuild it > Use Map and a function like #[[All, 2]] and then rebuild it. > > I don't like any solution. I wanted to know if there's anything simple > that I'm overseing Here are two that I often use: list = {{a, 5}, {b, 6}, {c, 7}}; Replace[list, {x_, y_} :> {x, y/2}, {1}] {#1, #2/2} & @@@ list where @@@ is a shortcat for Apply at level 1, see the documentation for details... hth, albert
From: Peter Pein on 13 Jun 2010 04:10 Am Sat, 12 Jun 2010 09:32:34 +0000 (UTC) schrieb Rui <rui.rojo(a)gmail.com>: > I have always done very ugly code when trying to do something > seemingly simple and basic such as applying a function to subparts of > a list. Simple example, I want to divide by 2 all the second elements > of a list > {{a, 5}, {b, 6}, ...} --> {{a, 5/2}, ...} > > My ideas: > Transpose -> MapAt/Map -> Transpose > Build the sublist with Part and then rebuild it > Use Map and a function like #[[All, 2]] and then rebuild it. > > I don't like any solution. I wanted to know if there's anything simple > that I'm overseing > > Thanks guys > If all pairs are of the form {name, integer} I would propose data/.{something_, i_Integer}:>{something, i/2} Peter
From: Leonid Shifrin on 13 Jun 2010 04:10 Hi Rui, The methods you listed are perfectly fine IMO. You may want to consider a rule-based solution as well: In[1]:= Replace[{{a, 5}, {b, 6}, {c, 7}}, {a_, b_} :> {a, b/2}, 1] Out[1]= {{a, 5/2}, {b, 3}, {c, 7/2}} Using ReplaceAll may fail in some cases since the rule based on generic patterns may sometimes be applied one level higher than desired: In[3]:= {{a, 5}, {b, 6}} /. {a_, b_} :> {a, b/2} Out[3]= {{a, 5}, {b/2, 3}} Regards, Leonid On Sat, Jun 12, 2010 at 2:32 AM, Rui <rui.rojo(a)gmail.com> wrote: > I have always done very ugly code when trying to do something > seemingly simple and basic such as applying a function to subparts of a > list. > Simple example, I want to divide by 2 all the second elements of a > list > {{a, 5}, {b, 6}, ...} --> {{a, 5/2}, ...} > > My ideas: > Transpose -> MapAt/Map -> Transpose > Build the sublist with Part and then rebuild it > Use Map and a function like #[[All, 2]] and then rebuild it. > > I don't like any solution. I wanted to know if there's anything simple > that I'm overseing > > Thanks guys > >
From: Bob Hanlon on 13 Jun 2010 04:12 data = Thread[Array[#, 3] & /@ {x, y}] {{x[1], y[1]}, {x[2], y[2]}, {x[3], y[3]}} data /. {a_, b_} -> {a, b/2} {{x[1], y[1]/2}, {x[2], y[2]/2}, {x[3], y[3]/2}} {1, 1/2} # & /@ data {{x[1], y[1]/2}, {x[2], y[2]/2}, {x[3], y[3]/2}} {#[[1]], #[[2]]/2} & /@ data {{x[1], y[1]/2}, {x[2], y[2]/2}, {x[3], y[3]/2}} Bob Hanlon ---- Rui <rui.rojo(a)gmail.com> wrote: ============= I have always done very ugly code when trying to do something seemingly simple and basic such as applying a function to subparts of a list. Simple example, I want to divide by 2 all the second elements of a list {{a, 5}, {b, 6}, ...} --> {{a, 5/2}, ...} My ideas: Transpose -> MapAt/Map -> Transpose Build the sublist with Part and then rebuild it Use Map and a function like #[[All, 2]] and then rebuild it. I don't like any solution. I wanted to know if there's anything simple that I'm overseing Thanks guys
From: Alexei Boulbitch on 15 Jun 2010 02:31 * /From/: Leonid Shifrin <lshifr at gmail.com> * /Date/: Sun, 13 Jun 2010 04:10:46 -0400 (EDT) ------------------------------------------------------------------------ Hi, Rui, this is another possibility: lst = {{a, 5}, {b, 6}, {c, 7}}; (* This is the function ...*) g[{x_, y_}] := {x, y/2} // N; (* ...to be mapped on the list *) Map[g, lst] {{a, 2.5}, {b, 3.}, {c, 3.5}} Have fun, Alexei Hi Rui, The methods you listed are perfectly fine IMO. You may want to consider a rule-based solution as well: In[1]:= Replace[{{a, 5}, {b, 6}, {c, 7}}, {a_, b_} :> {a, b/2}, 1] Out[1]= {{a, 5/2}, {b, 3}, {c, 7/2}} Using ReplaceAll may fail in some cases since the rule based on generic patterns may sometimes be applied one level higher than desired: In[3]:= {{a, 5}, {b, 6}} /. {a_, b_} :> {a, b/2} Out[3]= {{a, 5}, {b/2, 3}} Regards, Leonid On Sat, Jun 12, 2010 at 2:32 AM, Rui <rui.rojo at gmail.com> wrote: > I have always done very ugly code when trying to do something > seemingly simple and basic such as applying a function to subparts of a > list. > Simple example, I want to divide by 2 all the second elements of a > list > {{a, 5}, {b, 6}, ...} --> {{a, 5/2}, ...} > > My ideas: > Transpose -> MapAt/Map -> Transpose > Build the sublist with Part and then rebuild it > Use Map and a function like #[[All, 2]] and then rebuild it. > > I don't like any solution. I wanted to know if there's anything simple > that I'm overseing > > Thanks guys > > -- Alexei Boulbitch, Dr. habil. Senior Scientist Material Development IEE S.A. ZAE Weiergewan 11, rue Edmond Reuter L-5326 CONTERN Luxembourg Tel: +352 2454 2566 Fax: +352 2454 3566 Mobile: +49 (0) 151 52 40 66 44 e-mail: alexei.boulbitch(a)iee.lu www.iee.lu -- This e-mail may contain trade secrets or privileged, undisclosed or otherwise confidential information. If you are not the intended recipient and have received this e-mail in error, you are hereby notified that any review, copying or distribution of it is strictly prohibited. Please inform us immediately and destroy the original transmittal from your system. Thank you for your co-operation.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: How to center the position of a control in the Manipulate pane? Next: corrupted notebook |