RT.
julia> A = []
Any[]
julia> push!(A, "a")
1-element Vector{Any}:
"a"
julia> push!(A, "b")
2-element Vector{Any}:
"a"
"b"
julia> println(A)
Any["a", "b"]
How can I print A as [“a”, “b”], without Any? Thank you!
RT.
julia> A = []
Any[]
julia> push!(A, "a")
1-element Vector{Any}:
"a"
julia> push!(A, "b")
2-element Vector{Any}:
"a"
"b"
julia> println(A)
Any["a", "b"]
How can I print A as [“a”, “b”], without Any? Thank you!
What for exactly?
One way is
@show A
I’d like to print it as below:
[“a”, “b”]
not:
Any[“a”, “b”]
Thanks!
julia> println("["*join(A,",")*"]")
[a,b]
I thought that @show
didn’t show the Any
. (almost there above, I do not know of any solution which is just what you want)
For example, this works:
julia> println('[', join(repr.(A),", "), ']')
["a", "b"]
(Alternatively, using a loop and calling show
on the elements one by one, you could avoid allocating a bunch of strings just to print them as in the code above, but if the code isn’t performance-critical I wouldn’t worry about it.)
Thanks!
This doesn’t like a straightforward task in Julia as I thought. I need to learn more and understand why.
Just gave a try in Python, and Python outputs what I want.
>>> A = []
>>> A.append("a")
>>> A.append("b")
>>> A
['a', 'b']
For what purpose do you need/want this?
With a more precise type specified up front (good practice!), Julia works the same way:
julia> A = String[]
String[]
julia> push!(A, "a"); push!(A, "b"); println(A)
["a", "b"]
julia> @show A;
A = ["a", "b"]
Wow, that’s new to me. Thank you!
Python lists don’t have an element type, so there’s nothing to show.
No specific purpose. I just presumed Julia could do it same as or very similarly to other languages. Thanks
Thanks for explaining that.
As noted above with a specific element type you get the output that you want. With ‘Any’, you essentially get the type information printed along.
Maybe a different perspective is the following: To many people seeing this output will be very useful, because arrays of Any type will have poor performance and it’s probably good to highlight such arrays somehow?
To my knowledge, the type parameters of a container like an Array are fixed on initialization.
julia> A = [0]
1-element Vector{Int64}:
0
julia> push!(A, 1.)
2-element Vector{Int64}:
0
1
julia> push!(A, 1.5)
ERROR: InexactError: Int64(1.5)
julia> push!(A, "1")
ERROR: MethodError: Cannot `convert` an object of type String to an object of type Int64
To your point though, there is definitely some kind of logic for choosing to include the type when printing. I’m not sure if this is documented anywhere.
julia> println([0f0, 1f0])
Float32[0.0, 1.0]
julia> println([0., 1.])
[0.0, 1.0]
julia> println([0., "1"])
Any[0.0, "1"]
julia> struct Foo end
julia> println([Foo(), Foo()])
[Foo(), Foo()]
Can Python do this?
using Unitful
sgj(A) = println('[', join(repr.(A),", "), ']')
A = ["a", "b", 1.0, 1.0u"s", 2.0u"rad"]
push!(A, A[5]/A[4])
# note the last pushed element with proper units:
julia> sgj(A)
["a", "b", 1.0, 1.0 s, 2.0 rad, 2.0 rad s^-1]
Thanks. Beautiful code!
I know almost nothing about Python. I used that example to easily show what I wanted because I believe more Julia experts or learners here already have some experience with Python.