import numpy as np
from itertools import zip_longest
lst = [[1, 2, 3], [4, 5, 6, 7, 8], [9, 10, 11]]
def neighbors(a, b, m=lst, r=1):
m = np.array(list(zip_longest(*m)))#.T
v = np.argwhere(m == b) - np.argwhere(m == a)
return np.abs(v).max() <= r
assert neighbors(1, 2) == True
assert neighbors(1, 6) == False
assert neighbors(5, 6) == True
assert neighbors(3, 6) == True
assert neighbors(7, 11) == True
assert neighbors(1, 9) == False
assert neighbors(7, 8) == True
from collections import Counter
lst = ['a', 'c', 'f', 'r', 'a', 'a', 'f']
lst.sort() # не обязательно
result = Counter(lst)
import numpy as np
N = 20
numbers = np.random.randint(0, 40, N)
print('Исходный массив:', *numbers)
print('Уникальные пары: ', end='')
numbers.sort()
cache = set()
for index in np.argwhere(np.diff(numbers) == 1):
pair = numbers[index.item():][:2]
if cache.isdisjoint(pair):
cache.update(pair)
print(*pair, sep='-', end=' ')
Исходный массив: 28 10 24 19 17 0 20 24 5 30 26 39 6 38 31 39 28 25 18 24
Уникальные пары: 5-6 17-18 19-20 24-25 30-31 38-39
>>> import numpy as np
>>> np.math.factorial(np.arange(6))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: only size-1 arrays can be converted to Python scalars
>>> factorial = np.vectorize(np.math.factorial)
>>> factorial(np.arange(6))
array([ 1, 1, 2, 6, 24, 120])