Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

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

Search the Archive

Re: changing parts of a list of lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg52606] Re: changing parts of a list of lists
  • From: astanoff_otez_ceci at yahoo.fr (astanoff)
  • Date: Sat, 4 Dec 2004 04:07:46 -0500 (EST)
  • References: <comgeg$62q$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Dan Drake wrote:

> I'm working with a list of lists, such as {{1,2}, {3}, {4,5,6}}, and I
> want to go through and make changes to each sublist, but I get the error
> message "Set::setps: {{0,0,0},{0},{0}} in assignment of part is not a
> symbol."
> I don't quite understand what's going on. Here's a toy example that
> shows the error:
> f[x:{__List}] := For[i=1, i <= Length[x], i++, x[[i]] = {1, 2, 3}]
[...]

Dan,

Let me quote David B. Wagner : "the best of doing something [with 
Mathematica] 
is almost never the procedural way".

So you better not use For, Do or While to do what you want to do.

Why not simply use Map or MapAt?

In addition it's often better performance not to modify a large list in 
place.

For example if you want to prepend a zero to each of your sublists
you could do it this way :

In[1]:=myList = {{1,2},{3},{4,5,6}};

In[2]:=newList = Map[Prepend[#,0]&,myList]

Out[2]={{0,1,2},{0,3},{0,4,5,6}}


hth

Valeri



--
0% de pub! Que du bonheur et des vrais adhérents !
Vous aussi inscrivez-vous sans plus tarder!!
Message posté à partir de http://www.gyptis.org, BBS actif depuis 1995.




  • Prev by Date: Re: Finding the Fourier transform of discrete functions
  • Next by Date: A problem of numerical precision
  • Previous by thread: Re: changing parts of a list of lists
  • Next by thread: Invoking a function using full package name fail, but works without full package name