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

Пребарај: #decipher2024

当前筛选 #decipher2024清除筛选
DWF Labs Broadcast

@DWFLabs · Post #728 · 27.06.2024 г., 08:35

We had a great day yesterday at #Decipher2024 hosted by Algorand Foundation 🔥 Alessia Baumgartner, Business Partner of Ecosystems at DWF Labs, shared about what we look for in our portfolio and tips for pitches. Check out some key insights 👇 1️⃣ Pitch decks should be concise and compelling - communicating the ethos and important aspects of a project within the first two minutes. 2️⃣ Founders with strong technical skills and adaptability are integral to what we look for in projects, as they can adjust their strategies during market fluctuation to ensure success and longevity. 3️⃣ Projects that strongly fulfill key KPIs such as network activity and community engagement have a competitive edge. We're always looking for projects to support - feel free to reach out if you're working on a project, or have a project to share. Let's buidl! 🤝

DWF Labs Broadcast

@DWFLabs · Post #722 · 24.06.2024 г., 02:01

Catch Alessia Baumgartner, Business Partner of Ecosystems at DWF Labs, at #Decipher2024 hosted by Algorand Foundation. She'll be taking the stage at 4.15pm CET on June 26 to discuss what we look for in projects to support and partner with. Don't miss her panel.🔥

DWF Labs Broadcast

@DWFLabs · Post #725 · 26.06.2024 г., 13:57

Could you decipher where we are? 😁 Our Business Partner of Ecosystems, Alessia Baumgartner is taking the stage soon. If you're around and would like to hear more about what we look for in projects, come join us 🔥 #Decipher2024#Algorand#DWFLabs