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

Резултати

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

Пребарај: #veterinarystudent

当前筛选 #veterinarystudent清除筛选
#HaSab

@Hasab_2016 · Post #176 · 23.08.2025 г., 17:22

A step closer to a rabies-free community! I'm proud to share that I've been recognized as a Bronze-Level Dog Health Champion by the Global Alliance for Rabies Control (GARC). This certification fuels my passion for protecting both people and animals in our community. The best part? The training that got me here is completely free and available for everyone on the GARC website! This knowledge empowers us all to make a difference. Did you know that vaccinating 70% of dogs can break the cycle of rabies and protect everyone? As a DVM student, I am actively involved in educational activities to promote this and end the harmful practice of dog culling. I believe education is power! If you're a school, community group, or just a concerned pet owner, I'm here to help us learn together. Let's work towards the goal of #ZeroBy30! #CommunityHealth#DogVaccination#RabiesAwareness#GARC#DogHealthChampion#VeterinaryStudent#PublicHealth#LearnTogether#FreeEducation