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

Резултати

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

Пребарај: #phrases

当前筛选 #phrases清除筛选
English From Zero to Hero

@Learn_English_from_Zero_to_Hero · Post #60764 · 08.05.2026 г., 18:02

#phrases 💥Shallow matters ⚜The term "shallow matters" refers to topics, discussions, or concerns that lack depth, seriousness, or significance. They are often superficial and do not engage with deeper issues or complexities. ⚜While many people were debating the impact of climate change, Sarah seemed more interested in the shallow matters of which celebrity was dating whom."

Hashtags

English From Zero to Hero

@Learn_English_from_Zero_to_Hero · Post #60754 · 03.05.2026 г., 20:58

#Phrases 💢To cry out for 💠To be in desperate need of something 📝Note:- 📝 the subject is generally not a person, but an organisation, object or situation 🌀 Example:- A:- This room is crying out for some colour and modern furnishing. I suggested subtle green wallpaper, but it didn’t register with Jenny. On second thoughts, maybe red? B:- RED? You've definitely got a screw loose.

Hashtags

English From Zero to Hero

@Learn_English_from_Zero_to_Hero · Post #60753 · 03.05.2026 г., 20:58

#Phrases 💢 A chink in one’s armour 💠One small weakness in a person or their argument which makes them open to attack or criticism 🌀 Example:- Our lawyers have been sifting through the evidence but cannot find a chink in the defending company’s armour. It’s no wonder they are being very woolly on how they intend to present our case in court.

Hashtags

ПретходнаСтраница 1 од 4Следна