This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = np.arange(4).reshape((2,2)) | |
for i in np.nditer(a): | |
print(i) | |
>> 0 | |
1 | |
2 | |
3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = np.arange(4) | |
print(a,'\n') | |
>> [0 1 2 3] | |
b = np.reshape(a,(2,2)) | |
print(b, '\n') | |
>> [[0 1] | |
[2 3]] | |
c = b.reshape(4) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
np.random.seed(25) #setting a seed | |
print(np.random.rand(3)) | |
>> [0.87012414 0.58227693 0.27883894] | |
np.random.seed(25) | |
print(np.random.rand(3)) | |
>> [0.87012414 0.58227693 0.27883894] | |
print(np.random.rand(3)) | |
>> [0.52071879 0.32605113 0.69918624] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list_a, list_b = zip(*zipped_object) | |
>> list_a | |
(1, 2, 3, 4, 5) | |
>> list_b | |
('a', 'b', 'c', 'd', 'e') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list_a = [1, 2, 3, 4, 5] | |
list_b = ['a', 'b', 'c', 'd', 'e'] | |
zipped_object = zip(list_a, list_b) | |
>> type(zipped_object) | |
<class 'zip'> | |
>> list(zipped_object) | |
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lis = (i**2 for i in range(5)) | |
>> type(list) | |
<class 'generator'> | |
>> list(lis) | |
[0, 1, 4, 9, 16] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#example_1 | |
numbers = [1, 2, 3, 4] | |
squares = [n**2 for n in numbers] | |
>> squares | |
[1, 4, 9, 16] | |
#example_2 | |
obj = ["Even" if i%2==0 else "Odd" for i in range(5)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools #reduce() is in functools package | |
lis = [1, 3, 5, 6, 2] | |
def get_greater(a,b): | |
if(a>b): | |
return a | |
else: | |
return b |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list_a = list(range(5)) | |
def is_greater(n): | |
if(n>2): | |
return True | |
else: | |
return False | |
f1 = filter(lambda x : x>2, list_a) #lambda function | |
f2 = filter(is_greater, list_a) #normal function |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list_a = [1, 2] | |
list_b = [10, 20] | |
def add(a,b): | |
return a+b | |
m1 = map(add,list_a,list_b) #using normal function | |
m2 = map(lambda x, y: x + y, list_a, list_b) #using lambda function | |
>> print(list(m1)) |