TGTGInsighttelegram intelligenceLIVE / telegram public index
← Python Заметки

TGINSIGHT SIMILAR POSTS

Најди сличен содржај

Изворен канал @pythonotes · Post #146 · 16 сеп.

Функция sub в regex может принимать функцию в качестве аргумента repl. 📄 Из документации: If repl is a function, it is called for every non-overlapping occurrence of pattern. The function takes a single match object argument, and returns the replacement string. То есть для каждого совпадения будет вызвана функция для вычисления замены вместо замены на одну и ту же строку для всех совпадений. Иными словами, для замены разных совпадений на разные строки не потребуется запускать re.sub() много раз для каждой строки замены. Достаточно определить функцию, которая вернёт строку для каждого из совпадений. Описание слишком запутанное🤔, давайте лучше рассмотрим на простом примере: Создаем карту замены. То есть какие строки на какие требуется менять. remap = { 'раз': '1', 'два': '2', 'три': '3', 'четыре': '4', 'пять': '5', } Пишем функцию поиска строки для замены. Единственным аргументом будет объект re.Match. Используя данные этого объекта мы вычисляем замену on-the-fly! def get_str(match: re.Match): word = match.group(1) return remap.get(word.lower()) or word Пример текста. text = '''Раз Два Три Четыре Пять Вместе будем мы считать Пять Четыре Три Два Раз Мы считать научим вас ''' Теперь запускаем re.sub и вместо строки замены (repl) подаём имя функции. (Данный паттерн ищет отдельные слова в тексте) >>> print(re.sub(r'(\w+)', get_str, text)) 1 2 3 4 5 Вместе будем мы считать 5 4 3 2 1 Мы считать научим вас Думаю, достаточно наглядно 🤓 #libs#regex

Hashtags

Резултати

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

Пребарај: #darkacademia

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

@bookslibraryofficial · Post #1264 · 18.05.2025 г., 06:46

📖Title : Enigma ✍️Author : RuNyx ⭐️Rating : 3.91/5 (Goodreads) 📆Published : Apr 29, 2025 ———————————————— Summary:Enigma by RuNyx is a steamy dark academia romance that follows Salem Salazar, the black sheep of a wealthy family, as she enters Mortimer University to uncover the truth behind her sister's mysterious death. Obsessed with death and driven by secrets, Salem is soon entangled with Cazimir van der Waal—a chaotic, brooding artist and teaching assistant with a hidden past. Their intense, rivals-to-lovers relationship unfolds amid secret societies, missing girls, and deadly mysteries on campus. As danger closes in, passion ignites between fire and ice. *Enigma* is a twisted, suspenseful tale of obsession, love, and peril set in an academic underworld. ———————————————— #fiction#darkromance#fantasy#romantasy#darkacademia@Bookslibraryofficial@free_novellas@the_ancient_library

Velvet Pages | eLibrary 🥂

@bookslibraryofficial · Post #1430 · 09.09.2025 г., 14:32

📖Title : Katabasis ✍️Author : R. F. Kuang ⭐️Rating : 4.03/5 (Goodreads) 📆Published : Aug 26, 2025 ———————————————— Summary:Alice Law, an ambitious graduate student obsessed with becoming a leading magician, loses her mentor, Professor Jacob Grimes, in a magical accident—possibly her fault. Grimes’s soul is condemned to Hell, and Alice refuses to let death or damnation deny her future. Determined to rescue him, she risks her sanity, health, and very soul. Unfortunately, her bitter rival, Peter Murdoch, shares the same desperate mission. Forced into an uneasy alliance, the two must navigate Hell’s horrors together. Bound by ambition, rivalry, and guilt, they confront terrifying choices that test their humanity, their loyalty, and what they’re willing to sacrifice for power. ———————————————— #fantasy#darkacademia#fiction#romance#adult#mythology@Bookslibraryofficial@free_novellas@eternalmantra