Python Array Slicing | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

23rd Mar 2018, 12:29 PM
Aditya Rathore
Aditya Rathore - avatar
2 Answers
+ 2
in first cod you use NumPy - "np.array". in sec it's clear python. Need used - filter(lambda x: x > 0, a)
23rd Mar 2018, 2:46 PM
Viacheslav Prykhodko
Viacheslav Prykhodko - avatar
+ 1
Damn, I might have gone crazy learning Python, dafaq m doing, I now realize it's a list. Thanks man.
23rd Mar 2018, 2:49 PM
Aditya Rathore
Aditya Rathore - avatar