min_prices = dict()
for k, v in sweets.items():
min_prices[k] = dict()
for counter, i in enumerate(v):
if counter == 0:
min_prices[k]['price'] = i['price']
min_prices[k]['shop'] = i['shop']
continue
if i['price'] < min_prices[k]['price']:
min_prices[k]['price'] = i['price']
min_prices[k]['shop'] = i['shop']
pprint(min_prices)
{'карамель': {'price': 41.99, 'shop': 'магнит'},
'конфеты': {'price': 30.99, 'shop': 'магнит'},
'печенье': {'price': 9.99, 'shop': 'пятерочка'},
'пирожное': {'price': 59.99, 'shop': 'пятерочка'}}
min_prices = {k: min(v, key=lambda x: x['price']) for k, v in sweets.items()}
pprint(min_prices)
{'карамель': {'price': 41.99, 'shop': 'магнит'},
'конфеты': {'price': 30.99, 'shop': 'магнит'},
'печенье': {'price': 9.99, 'shop': 'пятерочка'},
'пирожное': {'price': 59.99, 'shop': 'пятерочка'}}
class Alrosa:
def __init__(self, page_source: str):
self.html = BeautifulSoup(page_source, 'html.parser')
@property
def citylist(self) -> List[str]:
return self._get_elements('.city')
@property
def pricelist(self) -> List[str]:
return self._get_elements('.price')
@property
def linkdeplist(self) -> List[str]:
elems = self._get_elements('.button a')
return [i.get('href') for i in elems]
def _get_elements(self, selector: str) -> List[str]:
return [elem for elem in self.html.selector(selector)]
def to_string(self, i: int) -> str:
return f'Маршрут: {self.citylist[i]}. Цена: {pricelist[i]}. Ссылка-{linkdeplist[i]}'
def get(url: str, headers: Dict[str, str]) -> requests.Response:
return requests.get(url, headers=headers)
r = get(url, headers)
alrosa = Alrosa(r.content)
for i in range(len(alrosa.citylist)):
print(alrosa.to_string(i))
>>> songs_titles = ['Песня1.mp3', 'Песня2.mp3', 'Песня3.mp3']
>>> songs = [{'title': s, 'file': f'https://site.com/{s}'} for s in songs_titles]
>>> songs
[{'title': 'Песня1.mp3', 'file': 'https://site.com/Песня1.mp3'}, {'title': 'Песня2.mp3', 'file': 'https://site.com/Песня2.mp3'}, {'title': 'Песня3.mp3', 'file': 'https://site.com/Песня3.mp3'}]
>>> import re
>>> s = """'#9f8757'
... '#d59005']
... '#edf8f0'
... '#77d525'
... '#C36728'
... ['#e1331b'
... '#b97601'
... '#aa9169'
... '#55672b'
... '#2684a6'
... '#e5d5bc'"""
>>> sl = re.findall(r"'(#.+)'", s)
>>> sl
['#9f8757', '#d59005', '#edf8f0', '#77d525', '#C36728', '#e1331b', '#b97601', '#aa9169', '#55672b', '#2684a6', '#e5d5bc']
>>> import pandas as pd
>>> df = pd.DataFrame([['123', 'Anime|Action'], ['321', 'Adventure|Comedy']], columns=['title', 'genre'])
>>> df
title genre
0 123 Anime|Action
1 321 Adventure|Comedy
>>> df['genre'] = df['genre'].apply(lambda x: x.split('|'))
>>> df
title genre
0 123 [Anime, Action]
1 321 [Adventure, Comedy]
>>> df.explode('genre')
title genre
0 123 Anime
0 123 Action
1 321 Adventure
1 321 Comedy
# as first layer in a sequential model: model = Sequential() model.add(Dense(32, input_shape=(16,))) # now the model will take as input arrays of shape (*, 16) # and output arrays of shape (*, 32) # after the first layer, you don't need to specify # the size of the input anymore: model.add(Dense(32))