Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia array of empty arrays

sorry for stupid simple question, newbie with Julia:

I would like to initialize array like this:

folds = [[], [], [], [], [], [], [], []], but not manually.

In Python I just write

folds = [[]] * 8, but this is not working with Julia.

How this is possible with Julia? I tried several times, but now success. Can you also explain to solution.

Thank you.

like image 248
gugatr0n1c Avatar asked Dec 02 '25 09:12

gugatr0n1c


2 Answers

One way would be a list comprehension (although this of course isn't quite as concise as Python):

[[] for i=1:8]
like image 115
Nils Gudat Avatar answered Dec 04 '25 02:12

Nils Gudat


The equivalent of folds = [[]] * 8 (which is probably not what you want!) in Julia is

folds = fill([], 8)

See the result of

push!(folds[1], 1)
8-element Array{Array{Any,1},1}:
 Any[1]
 Any[1]
 Any[1]
 Any[1]
 Any[1]
 Any[1]
 Any[1]
 Any[1]

and

folds[1].append(1)
# [[1], [1], [1], [1], [1], [1], [1], [1]]

On the other hand, a comprehension like [[] for i = 1:8] will create independent arrays in Julia and similar in python.

like image 45
tim Avatar answered Dec 04 '25 02:12

tim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!