Making a 1xN array using comprehensions

I mainly used

["something $i something" for i in 1:4].'

to make label arguments for multiple series (columns of a matrix) for Plots.plot. But this syntax is deprecated in v0.5.0.

Is there a similarly compact alternative? Using reshape is clumsy.

2 Likes

The deprecation warning suggests writing a specific transpose method, if appropriate. I don’t really know if it’s appropriate, but I tried it:

import Base.transpose
transpose(x::String) = x

Then your transpose seemed to work without warnings or errors (also without the dot).

I realized that

hcat(["something $i something" for i in 1:4]...)

and

["something $i something" for _ in 1:1, i in 1:4]

also work, but they ain’t gonna win any beauty contests either.

["something $i something" for i in collect(1:4)']

also works

That’s nice. No need to collect but the parentheses seem needed to transpose the range:

julia> ["something $i something" for i in (1:4)']
1×4 Array{String,2}:
 "something 1 something"  "something 2 something"  "something 3 something"  "something 4 something"
4 Likes

This. Is. Perfect. Thanks!