Задачи по питону и машинному обучению: алгоритмы, функции, классы, регулярные выражения, итераторы, генераторы, ООП, исключения, numpy, pandas, matplotlib, scikit-learn, TensorFlow и др. #Python #ml
class A:
def __init__(self, x):
self.x = x
def __call__(self):
return self.x
class B:
def __init__(self, x):
self.x = x
def method(self):
return self.x
class C:
def __init__(self, x=3):
self.x = x
def __repr__(self):
return str(self.x)
a = A(1)
b = B(2)
c = C(3)
callables = # ваш код
print([act() for act in callables])
#Вывод: [1, 2, 3]class A:
def __init__(self, x=0):
self.x = x
def f(self):
print(self.x)
a = A(2020)
f = a.f
print(f.__self__ is a, end=" ")
print(f.__func__ is f, end=" ")
print(f.__func__ is a.f, end=" ")
print(f.__func__ is A.f, end=" ")def f(x):
try:
print('1', end=" ")
y = 1 * 0 if x % 2 else 1 / 0
except:
y = 0
print('2', end=" ")
else:
print('3', end=" ")
finally:
print('4', end=" ")
return y
print(f(1) + f(2))from itertools import product a = [1, 2] b = [3, 4] sum(sum(product(a, b), (0,)))
import re
sent = 'Hello, Ben! How are you doing?'
lister = filter(None, re.split("[,!?]+", sent))
print(len(list(lister)))class A:
def __iter__(self):
yield from range(10)
a = A()
print(5 in a, 20 in a)class A:
x = 1
def __init__(self):
self.y = 2
a = A()a = 1; b = 2
print(eval('print(a + b, end=" ") or 0'))def f(n):
def g(x):
return x ** n
return g
g = f(3)
print(g(2), g(3))a = 0
def func():
global a
for num in [2, 4, 8]:
a += 1
yield num * 0.5
array = []
for val in func():
array.append(int(a == val))
print(sum(array))print(2 ^ 3 ^ 3 ^ 2 ^ 2 ^ 4 ^ 2 ^ 4 ^ 2)
def unpacking(x, y):
print(x, y, end=" ")
tuple_vec = (1, 0)
dict_vec = {'x': 1, 'y': 0}
unpacking(*tuple_vec)
unpacking(**dict_vec)c = filter(lambda x: x>=3, map(lambda x: x+x, (1, 2, 3, 4))) print(list(c))
c = map(lambda x: x + x, filter(lambda x: (x>3), (1, 2, 3, 4))) print(list(c))
x = {1: "A", 3: "B"}
y = {2: "C", 3: "D", 4: "E"}
d = {**x, **y}
vals = sorted(d.values())
print(*vals)