How can I convert a list such as:
a = {1, 2, 0.1}
into sequence so that I can pass them into a function, e.g. I want to be able to do something like this
Range[a]
so that the output will be:
1, 1.1, 1.2, ... , 2.0
How can I convert a list such as:
a = {1, 2, 0.1}
into sequence so that I can pass them into a function, e.g. I want to be able to do something like this
Range[a]
so that the output will be:
1, 1.1, 1.2, ... , 2.0
You can use the Range function in this way:
Range[1, 2, 0.1]
to get
{1.,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.}
If the problem is how to pass the parameter a
you can use:
1- Using Apply
.
a = {1, 2, 0.1}
Range@@a
The Apply
operator (@@
) "decapitates" the List
head from list a
and changes it by Range
.
2- Using Sequence
a = {1, 2, 0.1};
Range[Sequence @@ a]
It's useful when you have another variables to pass. That's not your case, but I just wrote to be more didactical.
Taking the last line of the question even more literally than Mr.Wizard
a = {1, 2, 0.1};
r = Range @@ a;
Row[{Sequence @@ r[[1 ;; 3]], "...", r[[-1]]}, ","]
1., 1.1, 1.2, ..., 2.
... you can also use ReplaceAll
:
a={1,2,.1};
a /. List->Range (* ReplaceAll *)
(* {1., 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.} *)
or, ReplacePart
:
ReplacePart[a, 0 -> Range]
(* {1., 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.} *)
or, set Part
zero to Range
(re-assign head):
b=a; b[[0]]=Range;b
Taking the last line of your question literally:
a = {1, 2, 0.1};
Row[Range @@ a, ","]
1., 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.
Also, to illustrate an alternative data format:
a = Sequence[1, 2, 0.1]
Range[a]
{1., 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.}
Also
a = {1, 2, 0.1};
Table[i, {i, ##}] & @@ a
(*{1., 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.}*)