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

Пребарај: #contact

当前筛选 #contact清除筛选

😄Contact ➖➖➖➖➖➖ 🔘Contact is a verb and means 'speak or write to someone' - often when someone wants to give or receive information. 🔜The doctor contacted me this morning to tell me the results of my tests. 🔜Please don't contact me with any more offers. 🔘Contact can also be used as a noun to talk about speaking or writing to someone. 🔘In contact with means 'speak or meet regularly'. 🔘Make contact means 'try to speak or write to someone after finding where someone is'. 🔘Lose contact means 'no longer communicate'. 🔜Stay in contact when you go away. 🔜After he left the city, I lost contact with him, but I'll try to make contact again if I find out his new address. 🔘Contact refers to a person who can help or give you information in work or socially. 🔜I have a contact at the company. Her name is Debbie. 🔜My contacts told me there's a job opening at the business. 🔘Contact as a noun refers to people whose names, numbers, and/or email addresses you have stored on your mobile phone. The list on your phone where you find this information is called contacts. 🔜I'll add you as a new contact on my phone. 🔜Look in my contacts. My brother's number is there. #Contact👨‍🏫@America ➖➖➖➖➖➖➖➖➖➖➖➖ 🆕 Crypto News @Money 😁 Crypto Game @Egame 🇺🇸 US News @America 🇯🇵 Japan News @Japan 🇦🇪 UAE News @Dubai ▶️ Popular Movies @Videos 😜 Best Funny Video @Funnys

Hashtags

dopingpong

@dopingram · Post #3162 · 03.08.2024 г., 06:58

«Лёгкий контакт», digital art, Doping Pong, 2001 Этот образ без преувеличения можно назвать классикой российского цифрового искусства. Предлагаем проследить преображение и эволюционные изменения, которые применили Doping Pong к этому сюжету на разных этапах творчества – от digital art до академической живописи. Легкий контакт - digital art, 2001 Легкий контакт (новая версия), digital art, 2005 Легкий контакт, бумага, пастель, 2015 год Легкий контакт, холст, акрил, 2018 год #dopingpong#ufo#space#love#alien#kiss#digitalart#russianart#light#contact