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

Пребарај: #paxton

当前筛选 #paxton清除筛选
Ultimora.net - POLITICS 24

@Ultimorapolitics24 · Post #52086 · 04.03.2026 г., 09:59

🇺🇸#USA – Si sono tenute in #Texas e #NordCarolina le primarie per le elezioni di metà mandato. Per il seggio al Senato in Texas, tra i dem ha vinto James #Talarico, seminarista presbiteriano noto per la sua comunicazione che unisce progressisti e moderati, battendo la liberal afroamericana Jasmine #Crockett. Talarico sfiderà il vincitore delle primarie repubblicane, molto combattute, tra il senatore John #Cornyn e il procuratore generale del Texas Ken #Paxton. @UltimoraPolitics24

America 🇺🇸 News & Politics

@America · Post #10343 · 07.03.2026 г., 05:34

🚨🇺🇸CORNYN DOMINATES TEXAS PRIMARY, PAXTON STUMBLES🤠 A massive political earthquake in the Lone Star State as Establishment GOP strikes back! 🐘⚡ 🔹Cornyn Crushes It: Sen. John Cornyn outperforms all expectations, securing a commanding lead despite attacks from the far-right. A huge win for Mitch McConnell's wing! 🏛️💪 🔹Paxton in Peril: Attorney General Ken Paxton, plagued by legal scandals & impeachment battles, is now fighting for his political life in a grueling runoff. The "America First" warrior is on the ropes! 🥊📉 🔹GOP Civil War: This primary exposes the deep rift between traditional Republicans and the MAGA insurgent wing in Texas. It's getting UGLY. 🩸🇺🇸 The runoff will be brutal. Can Paxton survive without Trump's full force? Or is Cornyn's victory a sign that the GOP is shifting? 🔄🔥 Texas politics is a bloodsport—grab your popcorn! 🍿👇 #USNews#Texas#politics#GOP#election#Cornyn#Paxton#CivilWar