+ 2
Python Array Slicing
I have the following code that executes without any problems: my_vector = np.array([-17, -4, 0, 2, 21, 37, 105]) zero_mod_7_mask = (my_vector % 7)==0 sub_array = my_vector[zero_mod_7_mask] sub_array[sub_array > 0] OUT: array([ 21, 105]) In the last line I am using '>' in the square brackets.. Now if I create a sinple array as below, it will give me an error while using '>': a = [1,2,3,4,5,6,35,67,89] a[a > 0] OUT: TypeError Traceback (most recent call last) <ipython-input-13-f6fbf6d9ee7c> in <module>() ----> 1 a[a > 0] TypeError: '>' not supported between instances of 'list' and 'int' Can anyone explain why it ran in the first eg and it didnt in the 2nd?' Thanks
2 Réponses