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

Пребарај: #hexagon

当前筛选 #hexagon清除筛选
Universe Mysteries 🪐

@cosmomyst · Post #18 · 29.07.2025 г., 21:51

🪐 High above the north pole of Saturn, the storm known as the "Hexagon" forms a perfect six-sided jet stream wider than two Earths across. Unlike any weather pattern on Earth, this mysterious geometric cloud system has maintained its shape for decades, swirling in icy isolation since first observed by Voyager and later by the Cassini spacecraft. ✨ #Saturn⚡#Hexagon⚡#atmosphere 👉subscribe Universe Mysteries

Addis Standard

@addisstandardeng · Post #21524 · 25.02.2026 г., 14:17

Will #Ethiopia be part of #Israel’s ‘hexagon’ alliance rivalling its enemies? Days after Israeli Prime Minister Benjamin #Netanyahu proposed forging a network of allied nations, including in the #Middle_East and #Africa, to stand against what he called “radical” adversaries, the country’s president is on an official visit to key ally, Ethiopia. It is not yet known which Arab and African countries will form part of Netanyahu’s hypothetical “#hexagon of alliances”, which he said on Sunday will include Israel, #India, #Greece, #Cyprus and others to stand against their enemies in the Middle East. Chief among those enemies is presumably #Iran and its network of resistance groups from #Hezbollah in #Lebanon to the #Houthis of #Yemen. Analysts doubt Israel could secure enough influence over nation-states to form a formal security pact. However, the country is deepening its ongoing charm.... https://web.facebook.com/AddisstandardEng/posts/pfbid02k6zhD7KwutXLnqV8A3PNCvX8aTQCduGEjMQcKEZKazJ7iuCCDwJdgwAZFULz2cMwl