One-dimensional Array access

I have an array:

a = collect(1:20)

and I wish to access elements 1:10 and 14. How is that done?
In python, with Numpy, I could do a[1:10,14]. Is there an accepted approach in Julia. Everything I tried has failed. I am also wanting to access non-contiguous columns in a Dataframe with 30 columns, say, columns 10:15 and 20:25.

Thanks, Gordon.

Hi!
This should work

a[[1:10...,14]]

and for the DataFrames:

select(df,10:15...,20:25...)

I learned something new!! I thought that … means any number of what preceded it.
What is the rationale for the use of the triple-dot in this way?

I solved my problem with:

@df df_ode plot(:t,

cols([2:6...,1]), # as an example

label=["S1" "S2" "S3" "S4" "S5" "S6"],
xlabel="Time",
ylabel="Number")

By the way, you actually can’t do that:

In [16]: a = np.array(range(20))                                                                                                                                                                                                           

In [17]: a[1:10, 14]                                                                                                                                                                                                                       
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-17-0de60f11d6ed> in <module>
----> 1 a[1:10, 14]

IndexError: too many indices for array

In [18]: a[range(0, 10), 14]                                                                                                                                                                                                               
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-18-0c6926e5d9b3> in <module>
----> 1 a[range(0, 10), 14]

IndexError: too many indices for array

The thing that actually works for me in Python is also what I would recommend in Julia:

In [19]: a[np.hstack((range(0, 10), 14))]                                                                           
Out[19]: array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 14])
julia> a = 1:20
1:20

julia> a[vcat(1:10, 14)]
11-element Array{Int64,1}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 14
2 Likes

... is sort of its own inverse operation:

  • In a function signature, f(args...) accepts any number of function arguments, collecting them as a tuple named args.
  • When used elsewhere, args... takes a tuple (or actually any iterable at all) and expands it to the same number of positional arguments.

Here’s an example of doing both:

julia> function f(args...)
         @show args
         +(args...)
       end
f (generic function with 1 method)

julia> f(1, 2)
args = (1, 2)
3

julia> f(1, 2, 3)
args = (1, 2, 3)
6

The function f takes any number of arguments. It receives them as a tuple named args. It then hands that tuple as multiple arguments to the function + (which also happens to take multiple arguments).

This is exactly the same as * in Python:

In [25]: def f(*args): 
    ...:     print("args:", args) 
    ...:     return sum([*args]) 
    ...:                                                                                                            

In [26]: f(1, 2)                                                                                                    
args: (1, 2)
Out[26]: 3

In [27]: f(1, 2, 3)                                                                                                 
args: (1, 2, 3)
Out[27]: 6
1 Like

A nicer syntax for this is

a[[1:10; 14]] 
6 Likes

Thank you everybody!! I really thought this would work in Python, but I stand corrected.
I guess the issue was mixing slices with non-consecutive indexes. I appreciate the help

Gordon

Depends what you mean, but it wasn’t the mixing, as such. You would have the same problem with

a[1:10, 11:20]

The issue is that you have to supply a single vector argument as index. You provided a list of two input arguments.

1 Like

For accessing DataFrames columns you can also use various combinations of Regex, All , Between and Not. You can find examples of usage in DataFrames Tutorial, specifically working with columns notebook.

1 Like

I understand, and it makes sense.