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

Резултати

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

Пребарај: #taehyung

当前筛选 #taehyung清除筛选
Bts.bighitofficial

@OUR_7_PRINCES · Post #115341 · 15.12.2025 г., 00:05

VOTE FOR #TAEHYUNG The Most Handsome Man In Asia 2025 /Asian Heartthrob ✅HOURLY https://nubiapage.com/the-most-handsome-man-in-asia-2025-asian-heartthrob/ Today is the last day vote fast

Hashtags

Bts.bighitofficial

@OUR_7_PRINCES · Post #115167 · 11.08.2025 г., 09:00

NEW COMPOSE COFFEE AD CAMPAIGN DETAILS! Compose Coffee announced on the 11th that it will launch three new menus of 'V COMPOSED' in collaboration with the exclusive model BTS V. COMPOSE COFFEE WILL BE ON THE THREE NEW LINEUPS OF 'VCOMPOSED' ON THE CONCEPT OF 'A CUP COMPOSED BY V'. The new lineup consists of Rich Allegro, Yuza Starcato, and Dolce Serenade, and was born with the motif of V's unique atmosphere and musical sensibility, who has been collaborating since 2023. Rich Alegro is a tea variation of green tangerine chamomile and rich, and the flavor of red grass is impressive. Yuza Starcato boasts a bitter and refreshing balance with Goheung yuza juice and grapefruit smoothie. Dolce Serenade added domestic honey and espresso to the blended milk of Compoz's unique. The 3 types of V COMPOSED will be available at Compose Coffee stores nationwide from the 12th. #Taehyung #INFO •@OUR_7_PRINCES•

123•••56
ПретходнаСтраница 1 од 6Следна