from heapq import heappush, heappop h = [] heappush(h, (5, 'a')) heappush(h, (7, 'b')) heappush(h, (3, 'c')) print(heappop(h)[1])
Задачи по питону и машинному обучению: алгоритмы, функции, классы, регулярные выражения, итераторы, генераторы, ООП, исключения, numpy, pandas, matplotlib, scikit-learn, TensorFlow и др. #Python #ml
from heapq import heappush, heappop h = [] heappush(h, (5, 'a')) heappush(h, (7, 'b')) heappush(h, (3, 'c')) print(heappop(h)[1])
from heapq import heappush, heappop
def f(iterable):
h = []
for value in iterable:
heappush(h, value)
return [heappop(h) for i in range(len(h))]
print(f([3, 5, 1]))import sys
def f(n):
if n == 0:
return 1
return n * f(n-1) # 1
sys.setrecursionlimit(1000) # 2
f(50) # 3
sys.setrecursionlimit(1) # 4
f(50) # 5recs = [(1, 2, 3), (1, 2), (3, 4), (0, 1, 2, 3, 4)]
s = 0
for a, *b in recs:
s += sum(b)
print(s)avg = lambda x: sum(x) / len(x)
def f(values):
a, *b, c = values
return avg(b)
f([0, 2, 3, 4])import heapq s = [2, 3, 0, 1] heapq.heapify(s) print(s[-1])
import heapq s = [2, 1, 3] heapq.heapify(s) print(s[0])
import heapq d = [5, -2, 3, 4, 1, 7 -3, 11, -9, 0, -1, 6] a = heapq.nsmallest(1, d) b = heapq.nlargest(1, d) print(*a, *b)
from collections import deque q = deque(maxlen=3) q.append(1) q.appendleft(2) q.append(3) q.append(4) q.popleft() print(q.popleft())
a, b, *c, d = (1,2,3,4,5,6) print(c)