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

Резултати

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

Пребарај: #honeybee

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

@interesting_planet_facts · Post #503 · 05.08.2025 г., 13:22

🌎 The Himalayan honey bee builds its hives on high cliffs, crafting giant honeycombs and producing reddish honey with unique psychoactive properties prized by local tribes. ✨ #honeybee⚡#Himalayas⚡#nature 👉subscribe Interesting Planet

Pensive|

@PensivePost · Post #5861 · 12.07.2022 г., 14:24

So yep, today my boyfriend called me two times on the line. Firstly, he started to call me when he woke up this morning. He was somewhat messy and had a bare face. He’s so lovable! No one knows I’m happy with how he treats me like he started a day with a face-to-face with me. We did enjoy it even tho the distance seemed a bit crazy. Secondly, In the afternoon… We hit up. As usual, we always bring up the topic of what we did today. I finally can share my happiness about the scarves that my momma brought me in the midday. He makes me a cutie-pie compliment, and I want to hear that a thousand times a day. Oh my goodness, he deserved what he wanted for me! Sincerely the one who always says mi luvu, Honey Lemon —July, 12th 2022 #review #maiworlds #maijournals #honeybee