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

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: [mg110318] Re: Apply a function to parts of a list
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Sun, 13 Jun 2010 04:10:24 -0400 (EDT)
  • References: <huvk7i$rtn$1@smc.vnet.net>

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


  • Prev by Date: Re: Lists: Row Vectors vs. Column Vectors. (feels like such a silly
  • 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