Re: Replace elements in structured list
- To: mathgroup at smc.vnet.net
- Subject: [mg99078] Re: [mg99062] Replace elements in structured list
- From: Adriano Pascoletti <adriano.pascoletti at dimi.uniud.it>
- Date: Sun, 26 Apr 2009 01:39:12 -0400 (EDT)
- References: <200904250852.EAA14320@smc.vnet.net>
Use ReplaceAll (/.) In[3]:= c = 1; data /. _?NumberQ :> c++ Out[3]= {{1, {2, 3, 4, 5}, {6, 7, 8, 9}, 10}, {11, {12, 13, 14, 15}, {16, 17, 18, 19}, 20}} Adriano Pascoletti 2009/4/25 <replicatorzed at gmail.com> > Dear Group, > > how would you solve this problem in an elegant and effective way? > My aim is to get a list of consecutive numbers, starting from 1, AND > to retain the structure of the original list as well. > > In[49]:= data = {{1, {2, 2, 2, 2}, {2, 2, 2, 2}, 1}, {1, {2, 2, 2, 2}, > {2, 2, 2, 2}, 1}}; > > c = 1; > > Replace[data, _?NumberQ :> c++, \[Infinity]] > > Out[51]= {{37, {38, 39, 40, 41}, {42, 43, 44, 45}, > 46}, {47, {48, 49, 50, 51}, {52, 53, 54, 55}, 56}} > > So the function should return a similar list, but starting at 1 > instead of 37. > Now I must note that the original data can have fewer/more levels, > elements, etc. > > Thank you in advance, > Istvan Zachar > >
- References:
- Replace elements in structured list
- From: replicatorzed@gmail.com
- Replace elements in structured list