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

Пребарај: #endthesiege

当前筛选 #endthesiege清除筛选
Freedom Flotilla Coalition

@FFC_official_channel · Post #282 · 22.05.2025 г., 08:57

We will sail. Since our ship was attacked on May 2, Israel has killed nearly 2,000 more Palestinians. Meanwhile, 14,000 babies now face imminent death from hunger due to Israel’s deliberate starvation campaign. This is the cost of global impunity. But we refuse to back down. The ‘Madleen’ will sail soon. She may be small, but her mission is powerful: To break the silence. To challenge Israel’s illegal blockade through nonviolent direct action. To stand firmly and unapologetically, with Gaza. JOIN US. Registrations are open for experienced and committed captains, crew, volunteers, and boat owners. You will be vetted and contacted only if successful: https://bit.ly/joinfreedomflotillaform To those who can help us on land, sign our petition for an investigation into the attacks, demand your governments ensure safe passage for the 'Madleen’, and share this journey - because the more eyes on us, the safer we are. #WeWillSail#Madleen#Conscience#FreeGaza#EndTheSiege#AllEyesOnDeck#StopArmingIsrael

Freedom Flotilla Coalition

@FFC_official_channel · Post #661 · 21.07.2025 г., 11:20

Amazon Labor Union leader Chris Smalls is on the 'Handala', the 37th ship to challenge Israel’s illegal siege on Gaza. Amazon has invested billions in intelligence like the Nimbus project, used for the surveillance and targeting of Palestinians. Chris shares that “…an injury to one is injury to all,” calling on labor unions worldwide to fulfill their responsibility: to be the shield for the working class, which includes protecting Palestinian people, and urges unions to stop their shipments of arms to Israel, fuelling their genocide in Gaza. “We come with baby food, toys, medicine, and hope — not just for Gaza, but for the future of our children in America back at home. The world is watching.” #FreePalestine#LaborForPalestine#EndTheSiege#Handala#UnionSolidarity#GazaFlotilla#AmazonComplicity