Equivalent for Python .join() function

551 views
Skip to first unread message

Alex

unread,
Jan 7, 2016, 5:47:31 AM1/7/16
to julia-users
Hello,

I have a collection of items which I need to write into the file line by
line:

A=[(item1_1, item1_2, ...), (item2_1, item2_2, ...), ...]

Line 1: item1_1, item1_2, ... \n
Line 2: item2_1, item2_2, ... \n
...

In order to do that I need to concatenate all elements in A[i] into the
string. In Python I would do it very quickly using .join function like that:

# List of tuples:
> a=[(1,2,"a"), (4,5,"g")]

# Join elements into the string
> " ".join(str(x) for x in a[0])
Out: '1 2 a'

> " ".join(str(x) for x in a[1])
Out: '4 5 g'

Is it some equivalent for this function in Julia?

--
Alex

David Salamon

unread,
Jan 7, 2016, 5:51:30 AM1/7/16
to julia...@googlegroups.com
Hi Alex,

I think you're looking for join.

julia> join(1:10, " - ")
"1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10"

If you haven't tried it, :methodswith and :methods are both amazing command line functions.

julia> methodswith(Array)
312-element Array{Method,1}:
 *{T,S}(s::Base.LinAlg.SVDOperator{T,S}, v::Array{T,1}) at linalg/arnoldi.jl:241
[lots of output -- can be used effectively if your terminal supports "find"]

julia> methods(join)
# 1 method for generic function "join":
join(args...) at strings/io.jl:104

Hope that helps,
David
Reply all
Reply to author
Forward
0 new messages