From: Rui on 12 Jun 2010 05:32 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: Bill Rowe on 13 Jun 2010 04:08 On 6/12/10 at 5:32 AM, rui.rojo(a)gmail.com (Rui) 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 Perhaps pattern matching, i.e., In[17]:= {{a, 5}, {b, 6}} /. {x_, y_Integer} -> {x, y/2} Out[17]= {{a, 5/2}, {b, 3}} But note In[18]:= {{a, 5}, {b, 6}} /. {x_, y_} -> {x, y/2} Out[18]= {{a, 5}, {b/2, 3}} This last happens since the pattern {x_,y_} matches {{a,5},{b,6}} which is why I restricted the match in the second position to integers. But if the list were longer, i.e., In[19]:= {{a, 5}, {b, 6}, {c, 7}} /. {x_, y_} -> {x, y/2} Out[19]= {{a, 5/2}, {b, 3}, {c, 7/2}} there is no problem with the simpler pattern
From: Carl K. Woll on 13 Jun 2010 04:09 On 6/12/2010 5:32 AM, Rui 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 > > With: list = {{a, 5}, {b,6}, {c,7}}; You can do: list[[All,2]] /= 2 and list will have all second elements cut in half. In[114]:= list Out[114]= {{a, 5/2}, {b, 3}, {c, 7/2}} Carl Woll Wolfram Research
From: David Park on 13 Jun 2010 04:07 Here is one method: mat = Transpose[{{a, b, c, d}, {5, 6, 7, 8}}] {{a, 5}, {b, 6}, {c, 7}, {d, 8}} MapAt[#/2 &, mat, Table[{i, 2}, {i, 4}]] {{a, 5/2}, {b, 3}, {c, 7/2}, {d, 4}} David Park djmpark(a)comcast.net http://home.comcast.net/~djmpark/ From: Rui [mailto: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
From: Sjoerd C. de Vries on 13 Jun 2010 04:09 Is this simple enough: In[41]:= list = {{"A", 1}, {"B", 2}, {"C", 3}, {"D", 4}, {"E", 5}, {"F", 6}, {"G", 7}, {"H", 8}, {"I", 9}, {"J", 10}}; In[42]:= list /. {i_, j_} -> {i, j/2} Out[42]= {{"A", 1/2}, {"B", 1}, {"C", 3/2}, {"D", 2}, {"E", 5/ 2}, {"F", 3}, {"G", 7/2}, {"H", 4}, {"I", 9/2}, {"J", 5}} ??? Cheers -- Sjoerd On Jun 12, 11:32 am, Rui <rui.r...(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
|
Next
|
Last
Pages: 1 2 3 Prev: How to center the position of a control in the Manipulate pane? Next: corrupted notebook |