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

Резултати

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

Пребарај: #newindia

当前筛选 #newindia清除筛选
MyGov Newsdesk

@MyGovCoronaNewsdesk · Post #13379 · 14.04.2025 г., 06:44

From Runways to Sky-High Stats ✈️ India’s passenger traffic soars 143% — a testament to rising infrastructure, connectivity & progress! #NewIndia

Hashtags

MyGov Newsdesk

@MyGovCoronaNewsdesk · Post #12790 · 05.06.2023 г., 13:02

Exciting opportunity to test your knowledge and contribute to nation-building 📝🧾 Take part in the '9 Years of Seva, Sushasan aur Garib Kalyan' Mahaquiz 2023 and celebrate the progress made towards #NewIndia. Visit: https://quiz.mygov.in/quiz/9-years-seva-sushasan-aur-garib-kalyan-mahaquiz-2023/

Hashtags

MyGov Newsdesk

@MyGovCoronaNewsdesk · Post #12691 · 10.05.2023 г., 08:52

Modern stations for a modern India! With state-of-the-art facilities & world-class amenities, the upgraded railway stations will be a reflection of #NewIndia's progress

Hashtags

MyGov Newsdesk

@mygovcoronanewsdesk · Post #12283 · 09.03.2023 г., 07:09

Capture the transformation of #NewIndia with your lens! Participate in contest and get a chance to win exciting cash prizes Visit: https://innovateindia.mygov.in/beneficiary-videos/

Hashtags

MyGov Newsdesk

@mygovcoronanewsdesk · Post #12181 · 27.02.2023 г., 11:14

Show #NewIndia with your lens! Participate in "Inviting Videos of Transformative Impact" and highlight the benefits received from particular schemes Visit: https://innovateindia.mygov.in/beneficiary-videos/

Hashtags

MyGov Newsdesk

@mygovcoronanewsdesk · Post #12143 · 23.02.2023 г., 15:11

Calling all creative minds to compose a Jingle for the 100th episode of Mann Ki Baat Participate & get a chance to make your jingle remain in the minds and hearts of #NewIndia Visit: https://secure.mygov.in/task/compose-jingle-100th-episode-mann-ki-baat/

Hashtags

MyGov Newsdesk

@mygovcoronanewsdesk · Post #12142 · 23.02.2023 г., 14:10

Show #NewIndia with your lens! Participate in "Inviting Videos of Transformative Impact" and highlight the benefits received from particular schemes Visit: https://innovateindia.mygov.in/beneficiary-videos/

Hashtags

MyGov Newsdesk

@mygovcoronanewsdesk · Post #12118 · 20.02.2023 г., 09:09

Here's a platform to showcase the #NewIndia Share your Before and After videos and describe how a particular scheme of government has benefited you Visit: https://innovateindia.mygov.in/beneficiary-videos/

Hashtags

MyGov Newsdesk

@mygovcoronanewsdesk · Post #11479 · 29.11.2022 г., 08:10

Are you the next best-selling author of #NewIndia? We are looking for you! Apply for YUVA 2.0 and get mentorship, stipend and much more! Know more here : innovateindia.mygov.in/yuva/

Hashtags

123•••10•••20•••2526
ПретходнаСтраница 1 од 26Следна