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 слични објави

Пребарај: #erica

当前筛选 #erica清除筛选
Trump's Ear

@trumpsear_tg · Post #1732 · 04.11.2025 г., 15:59

🇺🇸 In the USA, there are rumors around J.D. Vance and Charlie Kirk's widow Erika 💬. A new reason for speculation was their warm embrace 🤗 at a public appearance in the United States. For several days now, a short episode that took place on the stage of the University of Mississippi 🎓 has been causing a storm of discussion 🌪: a hug between Vice President J.D. Vance and Erika Kirk, the widow of Charlie Kirk, a shot activist and Trump supporter 🎤. At the Turning Point USA event 🇺🇸, Charlie Kirk's organization, now headed by Erika Kirk, Vance was invited as a speaker 🗣. When the vice president came on stage, Kirk greeted him with very warm hugs ❤️: Vance put his hands on her waist, and she ran her hand through his hair 💫. On social media 📱, they discuss not only the gesture itself, but also Erika's behavior in general after her husband's death 🕊. Some critics accuse her of inappropriate behavior and excessive openness ⚠️. Erika herself introduced J.D. Vance as "her good friend" who has similarities with her late husband 💔. #Vance#Kirk#Erica 👂More on Trump's Ear ⚠️