TGTGInsighttelegram intelligenceLIVE / telegram public index
← Python Заметки

TGINSIGHT SIMILAR POSTS

Најди сличен содржај

Изворен канал @pythonotes · Post #65 · 8 апр.

Небольшой трик с регулярными выражениями который редко вижу в чужом коде. Допустим, вам нужно распарсить простой текст и вытащить оттуда пары имя+телефон. Вернуть всё это надо в виде списка словарей. Возьмем очень простой пример текста. >>> text = ''' >>> Alex:8999123456 >>> Mike:+799987654 >>> Oleg:+344456789 >>> ''' Соответственно, для выделения нужных элементов будем использовать группы. Получится такой паттерн: (\w+):([\d+]+) Как мы будем формировать словарь из найденных групп? >>> import re >>> results = [] >>> for match in re.finditer(r"(\w+):([\d+]+)", text): >>> results.append({ >>> "name": match.group(1), >>> "phone": match.group(2) >>> }) >>> print(results) [{'name': 'Alex', 'phone': '8999123456'}, ...] Можно немного сократить запись используя zip >>> results = [] >>> for match in re.finditer(r"(\w+):([\d+]+)", text): >>> results.append(dict(zip(['name', 'phone'], match.groups()))) Но есть способ лучше! Это именованные группы в regex. Можно в паттерне указать имя группы и результат сразу забрать в виде словаря. >>> for match in re.finditer(r"(?P<name>\w+):(?P<phone>[\d+]+)", text): >>> results.append(match.groupdict()) То есть всё что я сделал, это добавил в начале группы (внутри сбокочек) такую запись: (?P<group-name>...) Теперь найденная группа имеет имя и можно обратиться к ней как к элементу списка >>> name = match['name'] Либо забрать сразу весь словарь методом groupdict() >>> match.groupdict() #tricks#regex

Резултати

Пронајдени 3 слични објави

Пребарај: #animalcare

当前筛选 #animalcare清除筛选
Google Facts™ [ ️@googlefactss🌎]

@googlefactss · Post #40739 · 05.03.2026 г., 15:01

Sheep can feel emotions like fear, anger, boredom, and happiness. They evaluate their surroundings using checks like suddenness, familiarity, predictability, and control, similar to humans. Sudden events can cause fear, while predictable environments may lead to boredom. 🐑⚽️ @googlefactss #AnimalWelfare#Sheep#Emotions#AnimalCare#Nature

EKOLOG.UZ| ENG

@ekologuzenglish · Post #11059 · 04.01.2026 г., 08:04

🐝 Could humanity survive only 4 years without bees? 🌱 Bees are among the most important living creatures on the planet. They pollinate plants, supporting the food chain. Without bees: 🍏 There would be fewer fruits and vegetables 💸 Food prices would rise 🐦 Ecosystems would be disrupted ⏳ According to some estimates, humanity could only survive normally for about 4 years due to food shortages Therefore: reduce pesticide use, protect nature, and create a safe environment for bees! 💚 #ProtectBees#Ecology#FoodSecurity#AnimalCare#PeacefulNature ☘️ Follow ecological news on the page @ekologuz 💬 Send your suggestions and ideas via @eklguz_bot

Home decor

@dzdigital3 · Post #18717 · 23.07.2025 г., 12:08

2025 EU hot selling cat litter box toilet box super wide easy to clean fully automatic wifi control smart cat litter box Get It By Aliexpress 🛒👇 https://s.click.aliexpress.com/e/_EHgBxVU Get It By Amazon 👇🛒 https://amzn.to/43qpFrc #animals#animallover#animalcare#cat#catlover#doglover#fypシ゚