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

Резултати

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

Пребарај: #advance

当前筛选 #advance清除筛选
America 🇺🇸 News & Politics

@America · Post #10161 · 29.11.2025 г., 02:42

😄Advance ➖➖➖➖➖➖ 🔘 Advance, the verb, can mean develop, improve and move forward. 🔜 You'll advance if you continue to study this hard. 🔘 Advance in this way can also be used as a noun. 🔜 There was an advance in sales when the software was updated. 🔘 Advance can be an adjective that means early. 🔜 I wish you'd given me advance warning of the redundancies. 🔘 If you ask for an advance, you ask to be given a payment before you are usually paid. 🔜 I asked for an advance because I had to pay a big bill. #Advance👨‍🏫@America ➖➖➖➖➖➖➖➖➖➖➖➖ 🆕 Crypto News @Money 😁 Crypto Game @Egame 🇺🇸 US News @America 🇯🇵 Japan News @Japan 🇦🇪 UAE News @Dubai ▶️ Popular Movies @Videos 😜 Best Funny Video @Funnys

Hashtags

America 🇺🇸 News & Politics

@America · Post #10153 · 27.11.2025 г., 03:05

😄Advance ➖➖➖➖➖➖ 🔘 Advance, the verb, can mean develop, improve and move forward. 🔜 You'll advance if you continue to study this hard. 🔘 Advance in this way can also be used as a noun. 🔜 There was an advance in sales when the software was updated. 🔘 Advance can be an adjective that means early. 🔜 I wish you'd given me advance warning of the redundancies. 🔘 If you ask for an advance, you ask to be given a payment before you are usually paid. 🔜 I asked for an advance because I had to pay a big bill. #Advance👨‍🏫@America ➖➖➖➖➖➖➖➖➖➖➖➖ 🆕 Crypto News @Money 😁 Crypto Game @Egame 🇺🇸 US News @America 🇯🇵 Japan News @Japan 🇦🇪 UAE News @Dubai ▶️ Popular Movies @Videos 😜 Best Funny Video @Funnys

Hashtags

Libertà è ragione

@libertaeragione · Post #4449 · 27.02.2026 г., 06:10

#Elezioni#RegnoUnito#Suppletive#GortonDenton Risultati definitivi: Affluenza: 47,6% Hannah #Spencer (#Greens|G/EFA): 40,6% Matt #Goodwin (#Reform|Destra populista): 28,7% Angeliki #Stogia (#LAB|S&D): 25,4% Charlotte #Cadden (#CON|Centro-destra): 1,9% Jackie #Pearcey (#LDEM|RE): 1,8% Sir Oink #ALot (#MRLP|Satirici): 0,4% Nick #Buckley (#Advance|Estrema destra): 0,4% Joseph #OMeachair (#RejoinEU|Europeisti): 0,3% Dan #Clarke (#LPUK|Libertari): 0,1% Sebastian #Moore (#SDP|Centro-sinistra conservatrice): 0,1% Hugo #Wils (#CL|Estrema sinistra): 0,1% Greens GUADAGNA su LAB. I Verdi inglesi vincono per la prima volta nella loro storia una elezione suppletiva. @OsservatorioEsteri