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

Пребарај: #ipprivacy

当前筛选 #ipprivacy清除筛选
Venture Village Wall 🦄

@venturevillagewall · Post #4189 · 19.02.2025 г., 10:00

New Developments in Tech and Crypto 🔸 MTS warns of risks due to the Russian government's IP data collection project. Read more 🔸 In Irkutsk, household electricity consumption spikes due to gray miners amid a mining ban and rising Bitcoin prices. Read more 🔸 Russian streaming services reportedly revive talks with Hollywood for content return, but no confirmations yet. Read more 🔸 Latest Telegram update includes conference interface for iOS users. Read more 🔸 Ex-CTO of OpenAI, Mira Murati, launches new AI startup, Thinking Machines Lab, focused on user-friendly AI adaptation. Read more 🔸 Intel stocks surge 16% amid rumors of a possible company split. Read more 🔸 Niantic, creator of Pokemon Go, aims to sell gaming business for $3.5 billion due to lack of success in replicating previous hits. Read more 🔸 Bitcoin ownership shifts: Private investors sold 525,000 BTC while institutional investors increased holdings by 374,000 BTC. source 🔸 Controversy in crypto space: Solana faces reputation challenges as scandals arise. More info #MTS#IPPrivacy#Bitcoin#Telegram#AI#OpenAI#Intel#Niantic#Crypto#Solana#Investment#B2B#ETFs#DataAnalysis#Tech#Gaming#Ecosystem#MarketTrends#Startups#VentureCapital#VC#Innovation