>>> import numpy as np
>>> from sklearn.cluster import KMeans
>>> notes = [145, 160, 112, 170, 281, 292, 258, 305, 257]
>>> notes = np.array(notes).reshape(-1, 1)
>>> KMeans(n_clusters=2).fit_predict(notes)
array([0, 0, 0, 0, 1, 1, 1, 1, 1], dtype=int32)
>>> KMeans(n_clusters=2).fit_predict(notes)
array([1, 1, 1, 1, 0, 0, 0, 0, 0], dtype=int32)
import hashlib
# hashlib.sha1(b'My_$uper&STRONG^p@ssw0rd').hexdigest()
PASSWORD_HASH = 'b0bda105892b7c25d4eb1e8e5cd36e942465ff69'
password = input("Введите пароль: ") # raw_input() для Python 2.x
if hashlib.sha1(bytes(password, 'UTF8')).hexdigest() == PASSWORD_HASH:
print('Password accepted')
else:
print('Wrong password')
with open('text.txt', 'rt') as file:
for index, line in enumerate(file):
if pattern in line:
print(index)
>>> matrix = np.array([[1, 2], [3, 4]])
>>> new_matrix = np.zeros((6, 6), dtype=matrix.dtype)
>>> new_matrix[1::3, 1::3] = matrix
>>> new_matrix
array([[0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 2, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 3, 0, 0, 4, 0],
[0, 0, 0, 0, 0, 0]])
>>> matrix = np.array([[1, 2], [3, 4]])
>>> matrix = matrix.reshape(1, 1, -1)
>>> matrix = np.pad(matrix, ((1, 1), (1, 1), (0, 0)), 'constant', constant_values=0)
>>> matrix = matrix.transpose(2, 0, 1).reshape(2, 2, 3, 3)
>>> matrix = np.hstack(np.hstack(matrix))
>>> matrix
array([[0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 2, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 3, 0, 0, 4, 0],
[0, 0, 0, 0, 0, 0]])
from calendar import Calendar
from itertools import product
cal = Calendar()
url = 'https://www.betexplorer.com/results/soccer/?year={}&month={}&day={}'
with open('dump.txt', 'wt') as file:
for year, month in product(range(2016, 2020), range(1, 13)):
for day in filter(None, cal.itermonthdays(year, month)):
print(url.format(year, month, day), file=file)
layer.weights
layer.get_weights()
for layer in model.layers[:4]:
layer.trainable = False
from itertools import groupby
from operator import itemgetter
# sid, symbol, rank
data = [
(36, 'abc', 5158),
(35, 'aaa', 4023),
(44, 'aaa', 3756),
(171, 'alc', 3262),
(179, 'soc', 701),
(42, 'abs', 3879),
(43, 'abs', 531),
]
def max_unique_rank(data):
_sid, _symbol, _rank = range(3)
filtered = sorted(data, key=itemgetter(_symbol, _rank), reverse=True)
filtered = groupby(filtered, key=itemgetter(_symbol))
filtered = [next(group) for _, group in filtered]
return filtered
max_unique_rank(data)
[(179, 'soc', 701),
(171, 'alc', 3262),
(42, 'abs', 3879),
(36, 'abc', 5158),
(35, 'aaa', 4023)]
cache = {}
for sid, symbol, rank in data:
_sid, _rank = cache.get(symbol, (sid, rank))
if _rank <= rank:
cache[symbol] = (sid, rank)
filtered = [(sid, symbol, rank) for symbol, (sid, rank) in cache.items()]
{symbol: (sid, symbol, rank) for sid, symbol, rank in sorted(data, key=itemgetter(2))}.values()
# phrase = input('Введите фразу: ')
phrase = 'А роза упала на лапу Азора'
phrase = list(filter(str.isalpha, phrase.casefold()))
if phrase == phrase[::-1]:
print('Палиндром')
else:
print('Не палиндром')
for item in text:
if item == ' ':
del item
np.array(img)
np.array(img.getdata(), np.uint8).reshape(img.size[0], img.size[1], -1)