Convert head Times to List
The listSample
containing the lists of multiplications must at all times remain wrapped in something that prevents it from being evaluated. So even the mere input line with which you define listSample
is not going to work.
When you Map
a function onto listSample
, the latter isn't wrapped the way it should be, so it gets evaluated. You can't use Map
directly on things that you would like to keep unevaluated. Fortunately, you don't need Map
in this case since you only want to replace Times
by List
.
So I'd suggest the following definition which makes the process re-useable:
SetAttributes[removeTimes, HoldAll];
removeTimes[list_] := ReleaseHold[Hold[list] /. Times -> List]
Now you can take your example list literally and wrap it in the above function:
removeTimes[
{12*12.5*13*13.5*14*14.5, 12*12.5*13*13.5*14*14.5,
12*12.5*13*13.5*14*14.5, 12*12.5*13*13.5*14*14.5,
12*12.5*13*13.5*14*14.5, 12*12.5*13*13.5*14*14.5}]
{{12, 12.5, 13, 13.5, 14, 14.5}, {12, 12.5, 13, 13.5, 14, 14.5}, {12, 12.5, 13, 13.5, 14, 14.5}, {12, 12.5, 13, 13.5, 14, 14.5}, {12, 12.5, 13, 13.5, 14, 14.5}, {12, 12.5, 13, 13.5, 14, 14.5}}
data=HoldForm@{12*12.5*13*13.5*14*14.5, 12*12.5*13*13.5*14*14.5,12*12.5*13*13.5*14*14.5, 12*12.5*13*13.5*14*14.5, 2*12.5*13*13.5*14*14.5, 12*12.5*13*13.5*14*14.5};
data/.Times->List // ReleaseHold
gives
{{12, 12.5`, 13, 13.5`, 14, 14.5`}, {12, 12.5`, 13, 13.5`, 14, 14.5`},
{12, 12.5`, 13, 13.5`, 14, 14.5`}, {12, 12.5`, 13, 13.5`,14, 14.5`},
{12, 12.5`, 13, 13.5`, 14, 14.5`}, {12, 12.5`, 13, 13.5`, 14, 14.5`}}
EDIT: An alternative:
timesToList = Function[{list}, Unevaluated[list] /. Times -> List, HoldAll]
timesToList[{12*12.5*13*13.5*14*14.5, 12*12.5*13*13.5*14*14.5}]
(* ==> {{12, 12.5, 13, 13.5, 14, 14.5}, {12, 12.5, 13, 13.5, 14, 14.5}} *)
The value assigned listSample
must be held unevaluated by some means or manipulation is not possible (as there is no Times
present). There are several options for this:
Hold
Unevaluated
SetDelayed
(:=
)
Of these SetDelayed
is the most concise, so I will use that:
listSample := {12*12.5*13*13.5*14*14.5, 12*12.5*13*13.5*14*14.5};
Normally this is fully evaluated as soon as you call listSample
:
listSample
{5.34398*10^6, 5.34398*10^6}
It is nevertheless stored internally in the unevaluated form. Because of this we can use Block
to temporarily change the meaning of Times
to List
while this evaluation is taking place:
Block[{Times = List}, listSample]
{{12, 12.5, 13, 13.5, 14, 14.5}, {12, 12.5, 13, 13.5, 14, 14.5}}
You could also access the unevaluated data with OwnValues
or with my step evaluation function which I wrote for this purpose.
step[listSample] /. Times -> List // ReleaseHold
This approach would be useful if you needed to do a more complex replacement than you could do with Block
, as would Hold
or Unevaluated
.
Also consider this:
Unevaluated[12*12.5*13*13.5*14*14.5] /. Times -> List
{12, 12.5, 13, 13.5, 14, 14.5}
Notice that no ReleaseHold
is needed in this case as Unevaluated
momentarily holds its argument, unlike Hold
which does so until released.