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

Пребарај: #novels

当前筛选 #novels清除筛选
Velvet Pages | eLibrary 🥂

@bookslibraryofficial · Post #1397 · 15.08.2025 г., 14:48

📖Title : The Return of the King ✍️Author : J.R.R. Tolkien ⭐️Rating : 4.58/5 (Goodreads) 📆Published : Oct 20, 1955 ———————————————— Summary:In The Return of the King, the final volume of J.R.R. Tolkien’s The Lord of the Rings, the forces of Gondor and Rohan prepare for the ultimate battle against Sauron. Aragorn embraces his destiny as the true king, rallying allies and marching to the Black Gate as a diversion. Meanwhile, Frodo and Sam struggle through Mordor to destroy the One Ring in Mount Doom. Gollum’s obsession leads to his and the Ring’s destruction, ending Sauron’s reign. Peace returns to Middle-earth, Aragorn is crowned, and the hobbits return to a changed Shire. Frodo later departs with the Elves to the Undying Lands. ———————————————— #fantasy#fiction#classics#adventure#novels#LOTR@Bookslibraryofficial@free_novellas@thesciencelabs

Velvet Pages | eLibrary 🥂

@bookslibraryofficial · Post #1391 · 04.08.2025 г., 15:29

📖Title : The Two Towers ✍️Author : J.R.R. Tolkien ⭐️Rating : 4.50/5 (Goodreads) 📆Published : Nov 11, 1954 ———————————————— Summary:In The Two Towers, the Fellowship is broken. Frodo and Sam continue their perilous journey to Mordor to destroy the One Ring, joined reluctantly by the treacherous yet pitiful Gollum, who becomes both guide and threat. Meanwhile, Aragorn, Legolas, and Gimli pursue the Uruk-hai who have captured Merry and Pippin. Their paths lead to the war-torn kingdom of Rohan, where they aid King Théoden against the forces of Saruman. As darkness spreads and war looms, alliances are tested and hope flickers. The story deepens the moral complexity of the quest, showing how power corrupts and loyalty defines true heroism. ———————————————— #fantasy#fiction#classics#adventure#novels#LOTR@Bookslibraryofficial@free_novellas@thesciencelabs

Telegram 公眾索引頻道

@TG_index_channel · Post #1484 · 27.04.2021 г., 01:40

標題:兽人小说|獸人小説 類別:#公開#頻道#簡中 分類:#閒聊#資源#社群#中文圈#NSFW 標籤:#novels#reading#獸人#furry#小説#閲讀 簡介:獸人小説 @FurryNovels 獸獸分享的獸人小説頻道 加群即可參與分享! #furry#novels#reading#NSFW #獸人#閲讀#小説 TG指南 te.legra.ph/TNTwwxs-09-08-06 中文群組 https://t.me/FurryNovels/27