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

Резултати

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

Пребарај: #league

当前筛选 #league清除筛选

🔔The New Year season of the Open League begins! 7th Season of The Open League starts today at 11:00 UTC. Anyone who wants to participate needs to get their #DeFi badges and read the terms of the rewards. The main focus of this season is liquidity💧 The season will last 20 days (from November 27 to December 17). The prize fund is $1.5M. The maximum reward for a participant is $1,000. Points can be earned in two categories: provided liquidity (TVL) and conducted trading turnover (Volume). The user's total points are the sum of the two categories. 💱Volume Points in this category are added for every $20 spent through League member projects. The dollar amount is calculated at the time of the exchange. ✔️GasPump — 5 points ✔️BigPump — 5 points ✔️Blum Mempad — 5 points ✔️TONPump — 5 points ✔️Wagmi — 5 points ✔️swap.coffee — 1 point ✔️Rainbow Swap — 1 point 🔒Liquidity | TVL Points in this category are added for every $20 invested in the liquidity of the sites that participate in the league. The dollar amount is calculated based on the value of the tokens at the end of the season. ✔️DAOLama — 15 points ✔️Aqua protocol — 15 points ✔️TonStable — 15 points ✔️TONCO — 10 points ✔️Coffin — 10 points ✔️JVault — 10 points ✔️Parraton — 10 points ✔️SettleTON — 10 points ✔️TonPools — 10 points ✔️TON Hedge — 10 points ✔️swap.coffee — 10 points for staking If you want to know how to get the most out of the season, join our chat for League tips. Leave your questions in the comments below this post. Get your badge now!🎁 #TON#SBT#AirDrop#League

跑跑資訊站 KartInfo

@KartInfoTW · Post #294 · 09.10.2021 г., 08:16

今晚五點,韓服 2021 跑跑聯賽第二季總決賽開打! 團體賽是文浩俊經營的戰隊「BLADES」對上循環賽一場都沒輸過但準決賽輸給 BLADES 的「Liiv SANDBOX」,個人賽則是有柳昌賢、朴仁秀和李載赫等多位明星選手登場,相信這次決賽肯定會相當精采 🔥 🏁 聯賽資訊/獎勵:https://kinf.cc/aSKLe ▶️ 追蹤 Google 新聞:https://kinf.cc/gn ▶️ 立即加入 Discord:https://kinf.cc/dc #跑跑卡丁車#KartRider#韓服#聯賽#跑跑聯賽#League#冠軍賽#總決賽