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 слични објави

Пребарај: #centralafricanrepublic

当前筛选 #centralafricanrepublic清除筛选
RTDocumentary

@rtdocumentary · Post #139 · 25.07.2021 г., 10:00

Testimony of an ex-child soldier #video#CentralAfricanRepublic In the Central African Republic, thousands of children have joined violent militias – either by force, or by choice. Since 2013, the country has been locked in civil war, with several disparate groups fighting each other. Some kids have joined the Christian Anti-balaka group and wear elaborate amulets to protect them against guns. Others fight for Seleka Muslim rebels with AK-47s. As part of a UNICEF-led effort to free child soldiers over the years, thousands of underage soldiers have left the groups. However, children traumatised by war have to go through a rehabilitation process before going home. This child who we filmed for Children of the Front shared his memories. Follow: https://t.me/rtdocumentary

Venture Village Wall 🦄

@venturevillagewall · Post #4111 · 10.02.2025 г., 07:00

Arweave AO Live, CAR Meme Token Launch Arweave’s computing platform AO is now active on its mainnet, enhancing decentralized applications. More info: The Block. In crypto news, the President of the Central African Republic, Faustin-Archange Touadéra, announced the launch of a meme token named CAR, reaching a market cap close to $900 million shortly after release. However, the token's legitimacy is questioned due to reported hacks and potential issues with its announcement video being AI-generated. Further details: Forklog. Analysts forecast significant declines for new token issuances, expecting drops of over 80% post-listing. Read more: Wu Blockchain News. #Arweave#DeFi#Crypto#Token#Market#Launch#CentralAfricanRepublic#MemeToken#Touadera#AI#Analyst#Predict#Listing#Invest#Blockchain#Ethereum#ETH