TGTGInsighttelegram intelligenceLIVE / telegram public index
← Python Заметки

TGINSIGHT SIMILAR POSTS

Најди сличен содржај

Изворен канал @pythonotes · Post #65 · 8 апр.

Небольшой трик с регулярными выражениями который редко вижу в чужом коде. Допустим, вам нужно распарсить простой текст и вытащить оттуда пары имя+телефон. Вернуть всё это надо в виде списка словарей. Возьмем очень простой пример текста. >>> text = ''' >>> Alex:8999123456 >>> Mike:+799987654 >>> Oleg:+344456789 >>> ''' Соответственно, для выделения нужных элементов будем использовать группы. Получится такой паттерн: (\w+):([\d+]+) Как мы будем формировать словарь из найденных групп? >>> import re >>> results = [] >>> for match in re.finditer(r"(\w+):([\d+]+)", text): >>> results.append({ >>> "name": match.group(1), >>> "phone": match.group(2) >>> }) >>> print(results) [{'name': 'Alex', 'phone': '8999123456'}, ...] Можно немного сократить запись используя zip >>> results = [] >>> for match in re.finditer(r"(\w+):([\d+]+)", text): >>> results.append(dict(zip(['name', 'phone'], match.groups()))) Но есть способ лучше! Это именованные группы в regex. Можно в паттерне указать имя группы и результат сразу забрать в виде словаря. >>> for match in re.finditer(r"(?P<name>\w+):(?P<phone>[\d+]+)", text): >>> results.append(match.groupdict()) То есть всё что я сделал, это добавил в начале группы (внутри сбокочек) такую запись: (?P<group-name>...) Теперь найденная группа имеет имя и можно обратиться к ней как к элементу списка >>> name = match['name'] Либо забрать сразу весь словарь методом groupdict() >>> match.groupdict() #tricks#regex

Резултати

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

Пребарај: #questwinner

当前筛选 #questwinner清除筛选
TRUE World Community

@true_ecosystem · Post #1203 · 23.03.2025 г., 08:00

ALL TREASURIES CLAIMED! ⚡️ What. A. Day. ⚡️ We honestly didn’t expect it to happen this fast — but you guys went full beast mode and claimed ALL 3 Treasuries in Bison Storm in just one day! Insane luck. Epic energy. Let’s give a thunderous shoutout to our Max Win legends: 1️⃣@mephostophil ▶️Watch the replay 2️⃣@felikyss ▶️Watch the replay 3️⃣@Alexey7878 ▶️Watch the replay Winners — DM @ze_bro to claim your treasure! Your ⚱️Bison Storm Treasuryis waiting. #questwinner ––––––––––––––––––– But wait — that’s not the end. You smashed it so hard, our CEO decided to join the party. This Sunday, he’s putting up his personal Treasury for a BONUS QUEST in Battle Rage! 🏆 1 Battle Rage Treasury — CEO Edition 🔥 Challenge details: ⚔️ Max Win in Battle Rage 💰 Minimum qualifying bet: 300 Coins ⏳ 1 winner only — first to hit takes it. One shot. One treasure. One legendary chance. Will you be the one to claim it? #weekendquest#battlerage

TRUE World Community

@true_ecosystem · Post #1202 · 22.03.2025 г., 18:08

BOOM! First Treasury Bison Storm just got claimed! @mephostophil hit the x25,000 Max Win and walked away with the prize. 1 down. 2 left. Your turn? #questwinner#weekendquest#bisonstorm Replay: https://launcher.grandgames.io/replay/bison-storm-96/biso9637002456bd724d78aaa4446779b9018c/cb4d3bbcd58ea5a328b490cb6298f0cf218e7ab679715b0aeb5a1bacce5effcc