May-26-2019, 03:29 PM
I hope this clarifies what's going on in there, but the best way is to check every part of the expression yourself by printing parts of it (or also modifying/playing with them) using interpreter
import numpy as np # np.array allows to use "array-oriented" math operations # For example using: # num_arr == 5 # Doing that to a list will return single "True" or "False" # Doing that to np.array will return another array containing "True" or "False" # corresponding to each value in the array # [5,10] == 5 evaluates to "False" # np.array([5,10]) == 5 evaluates to "array([True, False])" # So if you want to easily apply some operation (possibly using some numpy # functions) to an array then using np.array() makes it convenient. num_arr = np.array([0,1,2,3,4,5,1,2,3,4,5,2,3,4,5]) indices_where_5_is_found = np.where(num_arr[:-1] == 5)[0] + 1 print('Array:', num_arr, '\n') print('At which indices 5 was found:\n', indices_where_5_is_found, '\n') # Notice that if we didn't use [:-1] then the index of the last 5 # would also be included (resulting in additional empty sub-list), # [:-1] removes the last item from the array. print("At which indices 5 would be found if [:-1] wasn't used:\n", np.where(num_arr == 5)[0] + 1, '\n') print("Result if [:-1] wasn't used:\n", np.split(num_arr, np.where(num_arr == 5)[0] + 1), '\n') # If +1 wasn't used then 5 would be included as the first item of each # sub-list # np.array([1,2]) + 1 increases each item of the array by 1, resulting in: # array([2,3]) print("Indices if +1 wasn't used:\n", np.where(num_arr[:-1] == 5)[0], '\n') print("Result if +1 wasn't used:\n", np.split(num_arr, np.where(num_arr[:-1] == 5)[0]), '\n') arrays = np.split(num_arr, indices_where_5_is_found) print('Final result:\n', arrays, '\n')
Output:Array: [0 1 2 3 4 5 1 2 3 4 5 2 3 4 5]
At which indices 5 was found:
[ 6 11]
At which indices 5 would be found if [:-1] wasn't used:
[ 6 11 15]
Result if [:-1] wasn't used:
[array([0, 1, 2, 3, 4, 5]), array([1, 2, 3, 4, 5]), array([2, 3, 4, 5]), array([], dtype=int32)]
Indices if +1 wasn't used:
[ 5 10]
Result if +1 wasn't used:
[array([0, 1, 2, 3, 4]), array([5, 1, 2, 3, 4]), array([5, 2, 3, 4, 5])]
Final result:
[array([0, 1, 2, 3, 4, 5]), array([1, 2, 3, 4, 5]), array([2, 3, 4, 5])]