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

Пребарај: #gandhi

当前筛选 #gandhi清除筛选
TALKS

@talks · Post #19 · 29.07.2016 г., 17:05

◼ I've been fascinated by life and extraordinary achievements of Great Mahatma Gandhi. This man has freed India from British oppression using non-violent protests, which is a miracle in my opinion. Unfortunately it's very hard to find a good quality footage of Gandhi's speech, however there's an amazing Oscar winning biographical movie made in 1982. And i found a really nice piece for YOU to enjoy. #Gandhihttps://youtu.be/e3tjIiWIkAQ

Hashtags

Addis Standard

@addisstandardeng · Post #21655 · 09.03.2026 г., 13:46

#IndiGo flight from Delhi to Manchester makes sudden mid-air U-turn near #Ethiopia A #Delhi–#Manchester flight operated by IndiGo made an unexpected mid-air U-turn near the Ethiopia border on Monday, with the reason for the diversion still unclear. Flight 6E33, operated on a Boeing 787-9 Dreamliner, had departed from Indira #Gandhi International Airport in 3New_Delhi on a scheduled service to Manchester in the #United_Kingdom. The aircraft was en route over East Africa when it abruptly changed course and returned toward Indian airspace. Flight tracking data shows that the plane had approached the Ethiopian border before performing the sudden turnaround. There has been no indication of. a technical fault or operational issue, and no official explanation has been provided by the airline. Passengers on board were reportedly unaware of any immediate danger, and the flight later ..... https://web.facebook.com/AddisstandardEng/posts/pfbid0DLWYcW6KDf5PwDbLUSGtFnaFaqFRT4wKEQFRdFoMFMQxHJc2HrXHWDYoZz4FVugvl