2016-12-23 3 views
3

Ich habe 1-d Vektor von Werten. Ich möchte sie in eine Zeichenfolge mit durch Komma getrennten Werten konvertieren. Gibt es einen leichten Weg in Julia, dies zu tun? So etwas wie Zusammenbruch in rcollapse ein Vektor als Komma getrennte Zeichenfolge in Julia

{julia} 
julia> x = [24,122,63,24,83,56,54,175,11,11,24,51,85,92,74,500,80,127,126,59,111,155,132,202,64,164,1301] 

#I want output like this as a string 
#24,122,63,24,83,56,54,175,11,11,24,51,85,92,74,500,80,127,126,59,111,155,132,202,64,164,1301,27 

#I have tried something like this 
[julia> [print(i,",") for i in x] 
24,122,63,24,83,56,54,175,11,11,24,51,85,92,74,500,80,127,126,59,111,155,132,202,64,164,1301,27-element Array{Void,1}: 
nothing 
nothing 
nothing 
nothing 
nothing 
nothing 
nothing 
nothing 
nothing 
nothing 
nothing 
nothing 
nothing 
nothing 
nothing 
nothing 
nothing 
nothing 
nothing 
nothing 
nothing 
nothing 
nothing 
nothing 
nothing 
nothing 
nothing 

Antwort

3

drucken die meisten Werte mit einer gewöhnlichen Schleife, drucken Sie dann das letzte Element (das folgende Komma zu eliminieren):

julia> for i in @view x[1:end-1] 
      print(i, ',') 
     end; print(x[end]) 
24,122,63,24,83,56,54,175,11,11,24,51,85,92,74,500,80,127,126,59,111,155,132,202,64,164,1301 

Sie können auch jedem Element beitreten in der iterable mit einem Komma:

julia> print(join(x, ',')) 
24,122,63,24,83,56,54,175,11,11,24,51,85,92,74,500,80,127,126,59,111,155,132,202,64,164,1301 
+2

'join (map (string, x), ',')' ist genau das, was ich wollte. Danke – PoisonAlien

+1

@PoisonAlien - Sieht so aus, als ob die 'map' nicht notwendig ist, und Sie können' '' ('' '' '' '' 'direkt mitmachen). – TigerhawkT3

0

Auch diese, die einfacher ist: print(string(x)[2:(end - 1)]).

Verwandte Themen