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

Резултати

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

Пребарај: #economicgeography

当前筛选 #economicgeography清除筛选
Amazing Geography 🌍

@amazingeo · Post #675 · 28.03.2026 г., 20:31

🌍 Since the 1960s, Dubai’s desert coast has transformed into a global business hub through free trade zones, drawing global companies and turning once barren sands into a vibrant economic powerhouse. ✨ #economicgeography⚡#trade⚡#urbanization⚡#geography⚡#nature⚡#earth 👉subscribe Amazing Geography 👉more Channels ​

Amazing Geography 🌍

@amazingeo · Post #332 · 04.10.2025 г., 18:31

🌍 The Chicago area is the world’s largest concentration of inland rail and freight connections, making it a vital hub where goods from coasts and the Midwest converge before heading nationwide. ✨ #economicgeography⚡#transport⚡#trade⚡#geography⚡#nature⚡#earth 👉subscribe Amazing Geography 👉more Channels ​

Amazing Geography 🌍

@amazingeo · Post #178 · 05.09.2025 г., 00:12

🌍 More than half of global economic activity now takes place within just 1% of the world’s land surface, showing how powerful major cities and industrial regions are for global wealth. ✨ #economicgeography⚡#cities⚡#industry⚡#geography⚡#nature⚡#earth 👉subscribe Amazing Geography🌍 ​

Amazing Geography 🌍

@amazingeo · Post #292 · 24.09.2025 г., 18:31

🌍 Japan’s Tokaido corridor between Tokyo and Osaka packs over 60 million people and is a global hub for high-speed trains, industry, and services, shaping a megaregion central to Asia’s economy. ✨ #economicgeography⚡#megaregion⚡#infrastructure⚡#geography⚡#nature⚡#earth 👉subscribe Amazing Geography 👉more Channels ​