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

Пребарај: #wewantthemback

当前筛选 #wewantthemback清除筛选
Joti Brar

@jotibrar · Post #5936 · 11.03.2026 г., 12:59

A Women’s Day message from the Cilia Flores International Women's Brigades for Peace On 8 March, the world comes together to recognise our historic strength and our unwavering struggle. As women who uphold hope and justice in every corner of the world, we know that there is no true peace without freedom, nor feminism without internationalist solidarity. On this day of global struggle, we extend to you an invitation filled with feeling and commitment: Let us ensure that the name of our comrade Cilia Flores resonates in every corner of the globe and that, in your mobilisations and commemorative events for International Women’s Day, you proudly display banners with the phrase #BringThemBack#WeWantThemBack, accompanied by the image of our sister Cilia. May her face and our cry for justice be united with the millions of women who take to the streets of the world. For Cilia, for all women, for peace and freedom! We will prevail! Venceremos!