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

Резултати

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

Пребарај: #shapes

当前筛选 #shapes清除筛选
Universe Mysteries 🪐

@cosmomyst · Post #408 · 11.10.2025 г., 18:21

🪐 The galaxy NGC 2683, sometimes called the "UFO Galaxy," has a dramatic, warped disk that gives it a shape unlike typical spirals. Located about 35 million light-years away in the constellation Lynx, its tilted, edge-on view and slightly twisted arms create the illusion of a flying saucer drifting through space, making it one of the most striking examples of galaxies with unusual forms. ✨ #galaxies⚡#shapes⚡#astronomy⚡#nasa⚡#galaxy⚡#stars⚡#universe⚡#cosmos⚡#space 👉subscribe Universe Mysteries 👉more Channels ​

Universe Mysteries 🪐

@cosmomyst · Post #360 · 01.10.2025 г., 22:21

🪐 The galaxy NGC 4710, located about 60 million light-years away in the constellation Coma Berenices, is famous for its strikingly thin, edge-on profile that makes it look more like a cosmic needle than a typical spiral or elliptical galaxy. This unusual "boxy/peanut-shaped" bulge is thought to form when a flat disk of stars buckles and warps over time, creating a rare structure that reveals the hidden dynamics shaping galactic evolution. ✨ #galaxies⚡#shapes⚡#astronomy⚡#nasa⚡#galaxy⚡#stars⚡#universe⚡#cosmos⚡#space 👉subscribe Universe Mysteries 👉more Channels ​

Universe Mysteries 🪐

@cosmomyst · Post #275 · 15.09.2025 г., 13:11

🪐 The galaxy UGC 2885, sometimes called "Rubin's Galaxy," sports an unusual appearance with its outsized, sprawling disk—nearly 2.5 times wider than the Milky Way. Its vast, faint spiral arms stretch for over half a million light-years, making it not only one of the largest known spiral galaxies but also a cosmic oddity that challenges ideas about how galaxies can grow and maintain such immense, delicate structures. ✨ #galaxies⚡#mysteries⚡#shapes⚡#nasa⚡#galaxy⚡#stars⚡#astronomy⚡#universe⚡#cosmos⚡#space 👉subscribe Universe Mysteries ​

Universe Mysteries 🪐

@cosmomyst · Post #342 · 28.09.2025 г., 15:21

🪐 The galaxy known as the Cartwheel Galaxy, located about 500 million light-years away in the constellation Sculptor, displays a striking ring-and-spoke structure unlike ordinary spirals or ovals. Its unusual shape was created when a smaller galaxy crashed through its center, sending waves of star formation racing outward and leaving behind bright, circular rings and faint, radial arms—an extraordinary cosmic ripple frozen in space. ✨ #galaxies⚡#shapes⚡#collisions⚡#nasa⚡#galaxy⚡#stars⚡#astronomy⚡#universe⚡#cosmos⚡#space 👉subscribe Universe Mysteries 👉more Channels ​

Universe Mysteries 🪐

@cosmomyst · Post #256 · 11.09.2025 г., 13:11

🪐 The galaxy NGC 1427A, found in the Fornax Cluster, looks nothing like a neat spiral or smooth oval—it's shaped like a cosmic boomerang. This odd appearance is the result of NGC 1427A plunging through the dense cluster, which causes its stars and gas to be pulled and stretched, creating a lopsided, arrowhead-like structure as it slowly gets torn apart by gravity. ✨ #galaxies⚡#shapes⚡#mystery⚡#nasa⚡#galaxy⚡#stars⚡#astronomy⚡#universe⚡#cosmos⚡#space 👉subscribe Universe Mysteries ​

Universe Mysteries 🪐

@cosmomyst · Post #667 · 08.02.2026 г., 12:21

🪐 Not all galaxies fit the classic spiral or oval shapes—take the galaxy LEDA 074886, located about 70 million light-years away, which stunned astronomers with its rare rectangular structure. Unlike the familiar swirls or smooth ellipses, LEDA 074886’s boxy outline is thought to be the result of a merger between two smaller galaxies, sculpting a cosmic shape almost never seen in the universe. ✨ #galaxies⚡#mergers⚡#shapes⚡#nasa⚡#galaxy⚡#stars⚡#astronomy⚡#universe⚡#cosmos⚡#space 👉subscribe Universe Mysteries 👉more Channels ​

Universe Mysteries 🪐

@cosmomyst · Post #568 · 19.12.2025 г., 22:21

🪐 The galaxy ESO 137-001, streaking through the Norma Cluster about 220 million light-years away, has a spectacular "tadpole" shape with long blue tails streaming out behind it. These tails are made of hot, stripped gas and young stars that formed as the galaxy plunges through the dense cluster, showing how powerful cosmic forces can stretch and reshape galaxies far beyond the classic spirals and ovals. ✨ #galaxies⚡#shapes⚡#cluster⚡#nasa⚡#galaxy⚡#stars⚡#astronomy⚡#universe⚡#cosmos⚡#space 👉subscribe Universe Mysteries 👉more Channels ​

Universe Mysteries 🪐

@cosmomyst · Post #323 · 24.09.2025 г., 18:21

🪐 The galaxy known as the Tadpole Galaxy (UGC 10214), about 420 million light-years away in the constellation Draco, is famous for its long, sweeping tail that stretches over 280,000 light-years. This striking "tadpole" shape was created when another galaxy passed close by, pulling out a stream of stars, gas, and dust—a cosmic encounter that gave the Tadpole Galaxy its unforgettable, bizarre silhouette among the galaxies. ✨ #galaxies⚡#shapes⚡#collision⚡#nasa⚡#galaxy⚡#stars⚡#astronomy⚡#universe⚡#cosmos⚡#space 👉subscribe Universe Mysteries 👉more Channels ​

12
ПретходнаСтраница 1 од 2Следна