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

Резултати

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

Пребарај: #nowyouknow

当前筛选 #nowyouknow清除筛选
Google Facts™ [ ️@googlefactss🌎]

@googlefactss · Post #40710 · 27.02.2026 г., 07:04

Horseshoe crabs are not crabs at all. They are not even crustaceans. Instead, they have their own separate class named Merostomata. They have hardly changed since the age of the dinosaurs, and their closest-living relatives are spiders @googlefactss#nowyouknow

Hashtags

Google Facts™ [ ️@googlefactss🌎]

@googlefactss · Post #39972 · 29.10.2025 г., 08:48

In terms of absolute size and in proportion to overall body mass, the human penis is longer and thicker than that of any other primate. For instance the average size of the human penis is 8-10 cm when flaccid and 13-15 cm when erect while gorillas, which are the largest primates, have a penis size of approximately 4 cm in length when erect. @googlefactss#nowyouknow

Hashtags

Google Facts™ [ ️@googlefactss🌎]

@googlefactss · Post #39888 · 29.09.2025 г., 17:20

Greek philosopher Aristotle (384–322 BC) thought the heart, not the brain, was the center of mental functions, emotions, and intellect. He considered the heart the core of the body's "psychical fire" and the source of its heat, with the brain's main role being to cool the blood. @googlefactss#nowyouknow

Hashtags

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