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

Пребарај: #secondseason

当前筛选 #secondseason清除筛选
MoonPlay

@moonplay_official · Post #93 · 15.08.2024 г., 07:15

Let’s Conquer the Moon Together!🌕 Just landed on To The Moon!? Don’t worry - you’re joining at the perfect time as we kick off Season 3! Here’s how it works: 💸 mine Space Bucks, 🚀 upgrade your ship, 👥 invite friends & recruit a stellar crew, 🌌 play Star Dust to gain even more SP, 🏆 set off to the moon and level up! Every step you take brings you closer to token airdrop💸 after all 5 seasons. Focus on missions, levelling up, inviting referrals, and growing your space bucks balance. And for those of you who are starting out now, you’ve still got a shot at the airdrop by playing, conquering, and contributing to the mission. Ready to explore? Your adventure awaits! 👉TO THE MOON APP👈 Website | Telegram Chat | X | Discord | Medium #ToTheMoon#MoonPlay#ecosystem#blockchain#gamefi#web3#wenlisting#secondseason#communitymilestone#communityfirst#taptap#clicker#game

MoonPlay

@moonplay_official · Post #91 · 14.08.2024 г., 07:46

Shine Bright with Your Season 2 Badges and Win Big! 🪙 Season 2 was definitely one to remember: fierce competition, amazing Space Bucks balances, crazy level-ups 🆙, completed missions, and tons of new players joining To The Moon! via referral links 🤝. We've got plenty of reasons to celebrate! Just like we did in July - share your Achievement Badge from Season 2 2️⃣, and you could win a prize equal to the Space Bucks balance on it! Here's how to join in: 1️⃣ Follow the @moonplay_official group. 2️⃣Join our lively chat at @moonplay_chat. 3️⃣Drop a screenshot of your Season 2 Achievement Badge + use a hashtag #Season2 in the comments below this announcement. ⭐️To find your Season 2 Badge, just tap your profile in the top right and look under the Loot Capsules. We'll pick 20 lucky winners in 24 hours, so keep your eyes peeled! Good luck, and may your Badge shine the brightest! 👉TO THE MOON APP👈 Website | Telegram Chat | X | Discord | Medium #ToTheMoon#MoonPlay#ecosystem#blockchain#gamefi#web3#wenlisting#secondseason#communitymilestone#communityfirst#taptap#clicker#game