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

Резултати

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

Пребарај: #shows

当前筛选 #shows清除筛选
Car News

@car_news · Post #1225 · 25.04.2025 г., 09:47

📺Top Gear is back 📱 The first series of 'documentary comedy' The Not Very Grand Tour with the iconic show's presenters has been released. ℹ️The episode focuses on the memories of May, Hammond and Clarkson. It included an oil-car race through Detroit, a tank drive and the bombing of Clarkson's house. All in the name of internal combustion engines. 📆 There will be four episodes in total. The rest are due this year and 2026. @CarsNews #shows

Hashtags

One Piece (TV Series) Year of Production: 2023 Countries: USA, UK, Japan Filming Location: South Africa Genres: Adventure, Fantasy, Comedy, Action A fearless young captain assembles a diverse crew and embarks on an epic journey to become the King of the Pirates by uncovering a legendary treasure. This television series is a live-action adaptation of the iconic manga One Piece by Eiichiro Oda. The show received widespread acclaim from both critics and fans for its engaging storytelling and faithful adaptation. Part of the series was filmed at Cape Town Film Studios in South Africa. 🎥Official trailer. #movies#shows#series#culture