Re: Applying function to nested lists
- To: mathgroup at smc.vnet.net
- Subject: [mg57919] Re: Applying function to nested lists
- From: David Bailey <dave at Remove_Thisdbailey.co.uk>
- Date: Sun, 12 Jun 2005 04:34:49 -0400 (EDT)
- References: <d8e525$gnl$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Kerry Kim wrote: > Hello, > > I'm trying to get Mathematica to do the following: > > Suppose > x = {{1,2,3},{{4,5,6},7,8},9} > f = some function > > How can I get f applied to each element of x but with the list structure > intact? I.e. I want something that generates: > > {{f[1],f[2],f[3]},{{f[4],f[5],f[6]},f[7],f[8]},f[9]} > > Thank you! > -Kerry Kim > Hi, The problem here is that not all your numbers are at the same level with respect to the lists (otherwise, you could just use Map with a suitable levelspec), so one solution is to let f apply everywhere and then prune out the cases where f is applied to a list: Map[f,x,\[Infinity]]//. f[x_List]->x If f is actually a function that does something, use a function g that has no definition, and then replace g with f. This will work fine providing the data in your nested lists is simple numbers or variables, but if you might have algebraic expressions in there, then you would need a more complicated approach to avoid inserting f inside the expressions. David Bailey http://www.dbaileyconsultancy.co.uk