: column in front of variable

1

Oops, returning with the column question before the variable. Example I'm seeing in python in neural recurring. arr = arr[:n_batches * batches_size] What does :n_batches mean?

    
asked by anonymous 01.03.2018 / 01:53

1 answer

1

This is called slice . With it you can access a slice of an object that is iterable. For example, doing lista[1:5] you would be accessing items from 1 to 4 (5 represents the first index not included in the slice). When you omit the first value, the slice will be considered from the beginning of the iterator. That is, lista[:5] is the equivalent of lista[0:5] . If the second value is omitted, it will be considered until the end of it. That is, doing lista[5:] would get all values from index 5 to the end. If both numbers are omitted, it will take the iterator from start to finish, making a race copy of your object.

In your case,

arr = arr[:n_batches * batches_size]

You are accessing the iterable arr from the beginning to the result of the n_batches * batches_size multiplication. The equivalent would be:

arr = arr[0:n_batches * batches_size]

See some other examples:

lista = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

print(lista[1:5])  # [1, 2, 3, 4]
print(lista[:5])   # [0, 1, 2, 3, 4]
print(lista[1:])   # [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(lista[:])    # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

See working at Repl.it | Ideone

    
01.03.2018 / 02:09