Задачи по питону и машинному обучению: алгоритмы, функции, классы, регулярные выражения, итераторы, генераторы, ООП, исключения, numpy, pandas, matplotlib, scikit-learn, TensorFlow и др. #Python #ml
from collections import ChainMap
a = {'x': 1, 'z': 3 }
b = {'y': 2, 'z': 4 }
c = ChainMap(a, b)
a['x'] = 10
c['y'] = 20
c['z'] = 30
print(c['x'], b['y'], a['z'], b['z'])from collections import ChainMap values = ChainMap() values['x'] = 1 values = values.new_child() values['x'] = 2 values = values.parents print(values['x'])
from collections import ChainMap
a = {'x': 1}
b = {'x': 2}
c = {'x': 3}
d = ChainMap(a, b, c)
del d['x']
del d['x']
print(d['x'])from collections import ChainMap
a = {'x': 1, 'z': 3 }
b = {'y': 2, 'z': 4 }
c = ChainMap(a,b)
print(c['x'] + c['y'] - c['z'])from operator import itemgetter
ds = [{"x": 1}, {"x": 2, "y": 3}]
min1 = min(ds, key=itemgetter('x'))
min2 = min(i['x'] for i in ds)
print(min1 == min2)from collections import namedtuple
A = namedtuple('A', ['x', 'y'])
a = A(0, 0)
d = {'x': 1, 'y': 2}
a = a._replace(**d)
print(a.x + a.y)from collections import namedtuple
A = namedtuple('A', ['x', 'y'])
a = A('A', ['1', '2'])
a = a._replace(x=3)
print(a.x)from collections import namedtuple
A = namedtuple('A', ['x', 'y'])
a = A('A', ['1', '2'])
a.x = 3
print(a.x)from collections import namedtuple
A = namedtuple('A', ['x', 'y', 'z'])
a = A(1, 2, 3)
d = dict(x=1, y=2, z=3)
sa = a.__sizeof__()
sd = d.__sizeof__()from collections import namedtuple
Uni = namedtuple('Uni', ['num_student', 'num_groups'])
a = [(10, 4), (5, 3), (9, 5)]
def f(records):
res = 0.0
for r in records:
s = Uni(*r)
res += s.num_student * s.num_groups
return res
print(f(a))x = {'a' : 1, 'b' : 2, 'c': 3}
y = {t for t in x if t != 'a'}
z = {key:x[key] for key in x.keys() & y}
print(sum(z.values()))x = {'a' : 1, 'b' : 2, 'c': 3}
y = {t for t in x if t != 'a'}
z = {key:x[key] for key in x.keys() & y}
print(sum(z.values()))