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

Пребарај: #internationalmountainday

当前筛选 #internationalmountainday清除筛选
Space Universe🌌

@spaceuniverses · Post #28 · 27.12.2021 г., 15:00

Happy #InternationalMountainDay! ⛰ Hubble viewed its very own celestial "Mystic Mountain" in the Carina Nebula. This three-light-year-tall pillar of gas and dust is located about 7,500 light-years away from us. This infrared view shows reveals stars behind the gaseous veil of the nebula's wall of hydrogen, laced with dust. The foreground pillar becomes semi-transparent because infrared light from background stars penetrates through much of the dust. And if you haven't heard: The NASA Webb Space Telescope launches later this month, and with its powerful infrared capabilities, we’ll see spectacular infrared views of our universe! Image credits: NASA, ESA, and M. Livio and the Hubble 20th Anniversary Team (STScI)

Green University CA | Official

@centralasian_greenuniversity · Post #515 · 11.12.2025 г., 10:55

🏔️International Mountain Day 🗓Today, we celebrate International Mountain Day — a reminder of the incredible role mountains play in our lives. They provide fresh water, protect unique ecosystems, and inspire millions around the world. 🌱Let’s raise awareness, appreciate nature, and work together to preserve these powerful landscapes for future generations. #InternationalMountainDay#SustainableFuture#GreenUniversity#YouthForNature#EcoAwareness ✉️[email protected] ☎️ +998 55 512 00 77 📍Location 🌐Website | 📱Telegram | 📱Instagram | 📱LinkedIn | 🟥YouTube

🏔 11-декабрь — Эл аралык тоолор күнү 🏔 11 декабря — Международный день гор 🏔 December 11 — International Mountain Day #Kyrgyzstan#InternationalMountainDay#MountainsMatter #ProtectOurMountains#SustainableMountains #MountainLife#MountainCommunities #MountainEcosystems#UNMountainDay #LoveMountains#Mountains2025 https://youtu.be/7X56VFPic0s?si=3gZ6YYqkZ1L8v6G-