I am currently studying the functional programming part of Mathematica. But I am very confused right now about what I have learned so far.
This because I saw an example in textbook using Do
loop to swap list elements.
For example, original list :
lis = {{a, 1}, {b, 2}, {c, 3}}
Desired Result should be:
{{1, a}, {2, b}, {3, c}}
I try to write the program code as below, but no error, no result in the end.
lis = {{a, 1}, {b, 2}, {c, 3}};
Do[temp[i] = {lis[[i, 2]], lis[[i, 1]]}, {i, 1, Length[lis]}];
temp
For fixing this issue, I tried to rewirte line two as:
Do[temp[[i]] = {lis[[i, 2]], lis[[i, 1]]}, {i, 1, Length[lis]}];
But got errors as below which I also don't know why:
Set::noval: Symbol temp in part assignment does not have an immediate value.
General::stop: Further output of Set::write will be suppressed during this calculation.
So really appreciate anyone could clarify such problem. Thanks a ton!
Map[Reverse, lis]
? $\endgroup$temp
as a list to use this approach, eg puttemp = {0, 0, 0}
ortemp=ConstantArray[0,Length@lis]
before theDo
loop. $\endgroup$