MathGroup Archive 2010

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

Search the Archive

Re: Apply a function to parts of a list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg110312] Re: Apply a function to parts of a list
  • From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
  • Date: Sun, 13 Jun 2010 04:09:19 -0400 (EDT)
  • References: <huvk7i$rtn$1@smc.vnet.net>

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



  • Prev by Date: Re: Apply a function to parts of a list
  • Next by Date: Re: Apply a function to parts of a list
  • Previous by thread: Re: Apply a function to parts of a list
  • Next by thread: Re: Apply a function to parts of a list