Performing list(d) on a dictionary returns a list of all the keys used in the dictionary, in insertion order (if you want it sorted, just use sorted(d) instead). To check whether a single key is in the dictionary, use the in keyword.
list(dict.fromkeys(my_list))
class Element:
recipes = {
('Aqua', 'Air'): 'Storm',
('Aqua', 'Fire'): 'Steam',
('Aqua', 'Earth'): 'Dirt',
('Air', 'Fire'): 'Lightning',
('Air', 'Earth'): 'Dust',
('Fire', 'Earth'): 'Lava',
}
recipes.update({(b, a): c for (a, b), c in recipes.items()})
classes = {a for a, b in recipes.keys()} | {*recipes.values()}
@classmethod
def __str__(cls):
return cls.__name__
def __add__(self, other):
if isinstance(other, self.__class__):
return self
try:
return eval(self.recipes[(str(self), str(other))])()
except KeyError:
return None
for cls in Element.classes:
exec(f'class {cls}(Element): pass')
print(Air() + Aqua())
from itertools import takewhile, dropwhile, compress
for match in re.finditer('\[(?P<my_tag>[\w\d]+)\](.*?)\[/(?P=my_tag)\]', val):
if match:
print(match.group(2))
from collections import Counter
from operator import itemgetter
Counter(map(itemgetter('first_name'), students))
Counter(map(lambda student: student['first_name'], students))
def syllables(word, vowels=set('aeiou'), end='!'):
word = iter(word + end)
b, syllables = next(word), ''
while b != end:
a, b = b, next(word)
syllables += a
if (a in vowels) and (b not in vowels):
syllables += ' '
return syllables.split()
syllables('daabdhvoovuudpanda')
import requests
import pandas as pd
from lxml import html
url = 'https://zeon18.ru/page/search/?name=%D0%A2%D0%B5%D1%80%D0%BC%D0%BE%D0%BF%D0%B0%D1%81%D1%82%D0%B0'
tree = html.fromstring(requests.get(url).text)
def get_elements_by_xpath(xpath):
return [
html.tostring(element, method='text', encoding='unicode', with_tail=False)
for element in tree.xpath(xpath)
]
titles = get_elements_by_xpath('.//a[@class="catalog-item-title"]')
prices = get_elements_by_xpath('.//div[@class="catalog-item-price-main"]/span[@class="value"]')
pd.DataFrame(zip(titles, prices), columns=['title', 'price']).to_csv('catalog.csv', index=False)
items = ['abc', 'apple', 'pen', 'abc', 'pen', 'pc']
cache = {}
[cache.setdefault(item, len(cache)) for item in items]
from functools import lru_cache
from itertools import count
@lru_cache(maxsize=None)
def enumerate_as_seen(_, counter=count()):
return next(counter)
list(map(enumerate_as_seen, items))
a = [1, 2, 2, 3, 4, 3]
n = 2
list(zip(*[iter(a)]*n))
import numpy as np
np.array(a).reshape(-1, n)
import numpy as np
from itertools import count
def print_max_element(sequence, counter=count(0)):
print(f'Максимальный элемент в {next(counter)}-м столбце: {sequence.max()}')
np.apply_along_axis(print_max_element, 0, arr)
import numpy as np
a = [0, 1, 1, 2, 1, 3, 4, 4, 5, 4]
N = len(a)
a = np.array(a)
for n in range(3, N):
if N % n == 0:
b = a.reshape(-1, n)
b = np.diff(b, axis=1)
if np.all(b == b[0]):
print(n)
from operator import sub
a = [0, 1, 1, 2, 1, 3, 4, 4, 5, 4]
N = len(a)
for n in range(3, N):
if N % n != 0:
continue
matrix = list(zip(*[iter(a)]*n))
for i, row in enumerate(matrix):
matrix[i] = map(sub, row[1:], row[:-1])
transposed = zip(matrix)
row_to_set = map(set, transposed)
set_to_len = map(len, row_to_set)
if len(set(set_to_len)) == 1:
print(n)
arr[ arr < 17 ].tolist()