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

Резултати

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

Пребарај: #primeminister

当前筛选 #primeminister清除筛选
MessageInABottle

@mib_messageinabottle · Post #7072 · 08.08.2024 г., 23:31

#AI generated #UK#PrimeMinister Keir #Starmer video. It is basically what he has demonstrated and stated, even if not in these exact words. It's clear if you are white working class in the #UK you do not have any meaningful right of protest, be angry, demonstrate or have an opinion. Whoever made this has just translated #Starmer's bullshit into what he really means when he talks about "#FarRight thugs"

MessageInABottle

@mib_messageinabottle · Post #7073 · 08.08.2024 г., 23:57

Compare and contrast #UK#PrimeMinister#Starmer's reaction to: • "firey but mostly peaceful" #BLM riots in the US after the death of petty criminal drug-addict George Floyd Vs •#White Working class "Anti-#Stabbing" riots in the #UK after the #murder of 3 #children

Crypto M - Crypto News

@CryptoM · Post #64747 · 09.04.2026 г., 17:47

🚀 UK Prime Minister Criticizes Trump's Rhetoric on Iran UK Prime Minister Keir Starmer has expressed disapproval of U.S. President Donald Trump's recent comments threatening to destroy Iran. Bloomberg posted on X, highlighting Starmer's assertion that such rhetoric does not align with British values. The Prime Minister emphasized the importance of diplomatic approaches and maintaining international peace, suggesting that aggressive language could undermine efforts to resolve tensions. Starmer's remarks come amid ongoing global discussions about Iran's nuclear activities and regional stability. The UK government continues to advocate for dialogue and cooperation in addressing international security concerns. #UK#PrimeMinister#KeirStarmer#Trump#Iran#Diplomacy#InternationalRelations#Peace#GlobalSecurity#NuclearActivities