May-25-2019, 05:45 PM
(This post was last modified: May-25-2019, 05:48 PM by python_newbie09.)
you are very helpful! thank you very much.
btw, could explain what is actually happening in this line of code, especially the last part [0]+1
(May-25-2019, 11:27 AM)michalmonday Wrote:num_list = [0,1,2,3,4,5,1,2,3,4,5,2,3,4,5] arrays = [[]] # array of sub-arrays for i, num in enumerate(num_list): # go through each element after the first arrays[-1].append(num) # Add it to the last sub-array if num == 5 and i != len(num_list)-1: # if 5 encountered and not last element arrays.append([]) print(arrays)
Output:[[0, 1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [2, 3, 4, 5]]import numpy as np num_arr = np.array([0,1,2,3,4,5,1,2,3,4,5,2,3,4,5]) arrays = np.split(num_arr, np.where(num_arr[:-1] == 5)[0]+1) print(arrays)
Output:[array([0, 1, 2, 3, 4, 5]), array([1, 2, 3, 4, 5]), array([2, 3, 4, 5])]
btw, could explain what is actually happening in this line of code, especially the last part [0]+1
arrays = np.split(num_arr, np.where(num_arr[:-1] == 5)[0]+1)