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

Резултати

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

Пребарај: #artifacts

当前筛选 #artifacts清除筛选

On the Schöningen Double-Pointed Sticks In Schöningen, Germany, archaeologists found double-pointed sticks, crafted 200,000 years ago by early Europeans, likely Neanderthals. These slender wooden tools, possibly throwing sticks, showcase precise craftsmanship. Sharpened at both ends, they reflect sophisticated woodwork and hunting skill. Their balanced design suggests aerodynamic mastery for small prey. Preserved in sediment, these artifacts highlight Europe’s ancient ingenuity. #artifacts @EuropeanTribalism

Hashtags

Altar dedicated to the divine twins Mars and Venus. Marble, Roman artwork of the end of the reign of Trajan (98-117 AD), later re-used under the Hadrianic era (117-132 AD) as a base for a statue of Silvan. From the portico of the Piazzale dei Corporazioni in Ostia Antica. Shown in museum of Palazzo Massimo alle Terme, Rome, Italy #artifacts @EuropeanTribalism

Hashtags

On swastikas (18) In 2020 scientists discovered a trove in Denmark, which contained around one kilogram of gold artifacts. Among these items there was a disc depicting the Norse god Odin. The interesting thing is, that this disc can be dated to the 5th century A.D., and is thus the oldest trace of Odin worship which was ever found. The runic inscription on the disc reads "He is Odin's man". Interestingly the swastika is clearly depicted next to Odins head in the center of the disc. #artifacts @EuropeanTribalism

Hashtags

123•••67
ПретходнаСтраница 1 од 7Следна