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

Резултати

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

Пребарај: #butterflies

当前筛选 #butterflies清除筛选
Interesting Planet 🌍

@interesting_planet_facts · Post #1129 · 14.12.2025 г., 18:11

🌎 Spectacular animal migrations include the annual journey of monarch butterflies, which travel up to 4,800 kilometers from Canada and the U.S. to central Mexico. These butterflies navigate using a combination of environmental cues and an internal sun compass in their antennae. ✨ #migration⚡#butterflies⚡#nature 👉subscribe Interesting Planet 👉more Channels ​

Interesting Planet 🌍

@interesting_planet_facts · Post #1388 · 29.04.2026 г., 12:11

🌎 Monarch butterflies complete a multigenerational migration from Canada to central Mexico, traveling up to 4,500 kilometers. No single butterfly makes the entire round trip; several generations are born and die along the way. ✨ #butterflies⚡#migration⚡#insects 👉subscribe Interesting Planet 👉more Channels ​

Interesting Planet 🌍

@interesting_planet_facts · Post #1282 · 08.02.2026 г., 22:11

🌎 Each year, monarch butterflies migrate up to 4,800 kilometers from Canada and the northern United States to overwinter in central Mexico. Clusters of millions gather on oyamel fir trees, using environmental cues like the angle of sunlight to navigate. ✨ #migration⚡#butterflies⚡#insects 👉subscribe Interesting Planet 👉more Channels ​

Interesting Planet 🌍

@interesting_planet_facts · Post #756 · 15.09.2025 г., 18:22

🌎 Monarch butterflies in North America complete one of the longest insect migrations, traveling up to 4,800 kilometers from Canada and the U.S. to central Mexico. Each fall, millions gather in mountain forests, forming dense colonies that help them survive winter’s cold. ✨ #butterflies⚡#migration⚡#insects 👉subscribe Interesting Planet ​

Interesting Planet 🌍

@interesting_planet_facts · Post #549 · 12.08.2025 г., 20:22

🌎 Some butterflies taste with their feet! Tiny sensors on their legs let them “sample” leaves, ensuring they lay eggs only on the best plants for their caterpillars. ✨ #butterflies⚡#senses⚡#nature 👉subscribe Interesting Planet

12
ПретходнаСтраница 1 од 2Следна