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

Резултати

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

Пребарај: #singsingglobal

当前筛选 #singsingglobal清除筛选
SingSing Announcement

@singsingchannel · Post #639 · 18.12.2023 г., 13:45

🎅 🎅Join Our Special Christmas Event 🎅 🎅 🎁We are excited to announce a special event for the SingSing community! In celebration of the warm Christmas holiday, we are offering you the opportunity to participate and win attractive rewards. 🎄Event Details 🎄 🎊 Submit your recorded songs using the SingSing app to Facebook, Twitter, or Instagram with the hashtag #singsingglobal. And the one that gets the most likes will win. 🎁Prize for the most-liked song: $20 + 5,000 SING tokens. 🎁Prize for the second most-liked song: $15 + 3,000 SING tokens. 🎁Prize for the best song as voted by the CM and MOD team: $10 + 2,000 SING tokens + Wood Microphone. 🎊 Results will be announced on December 25, 2023. 🎄How to Participate 🎄 1. Record your song using the SingSing app. 2. Share your song on social media (FaceBook, Instagram, Twitter) with the hashtag #singsingglobal. 3. Stay tuned for the results on December 25, 2023. 🎁Let's celebrate the festive spirit together and showcase your musical talent through this event. Wishing you all a happy and cozy Christmas season 🎊 ➡ ➡ Join our community for more 👈 👈