Francis Smart
Francis Smart

Reputation: 4055

Julia: Using split to construct string arrays with multiple columns

I have a tuple which contains two pieces of information separated by # that looks like x = ("aa#b", "a#c", "a#d"). I can use a comprehension to transform this data into an array in the following way [split(x[i], "#")[j] for i in 1:length(x), j in 1:2].

However, this seems inefficient since I am effectively running the split command twice. Is there a preferred way of handling this case?

Thank you

Upvotes: 0

Views: 675

Answers (2)

Michele Garau
Michele Garau

Reputation: 88

Despite it being an old question, here it is my proposed solution

String.(reduce(hcat,split.(a,'#')))

I checked with the @time operator. Compared with the list comprehension proposed by Alexander, which takes 0.042378 seconds (90.93 k allocations: 4.330 MiB), the amount of time required for the same operation with the proposed solution is 0.000028 seconds (34 allocations: 1.500 KiB).

Upvotes: 1

Alexander Morley
Alexander Morley

Reputation: 4181

function hashsplit(x)
       out = Array{SubString{String},2}(2,length(x))
       for (ind,j) in enumerate(x)
           out[:,ind] = split(j,"#")
       end
       return out
end

Should be faster. Else a simple way with a list comprehension would be

[(split(x[i], "#")...) for i in eachindex(x)] (for a vector of tuples)

cat(2,ans...) or reduce(hcat, ans) if you want a matrix.

Upvotes: 3

Related Questions