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

Резултати

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

Пребарај: #mhev

当前筛选 #mhev清除筛选
Auto Life

@revheadcrypto · Post #423 · 02.09.2024 г., 13:52

▎🚘 Introducing the All-New Audi Q5! The next generation of the Q5 has arrived, now built on the PPC (Premium Platform Combustion), shared with the recently unveiled Audi A5. The design is completely new, yet resonates with the familiar aesthetic of Audi’s electric models. 🎮Inside the Cabin: Featuring three stunning displays on the central panel, the media system runs on Android OS. The compact gear selector enhances the sleek design, while physical climate control buttons have been eliminated for a more modern feel. ⚡️Power & Performance: In Europe, all Audi Q5 engines will come equipped with the MHEV Plus mild-hybrid system, featuring a 24 hp electric motor and a compact battery. The base versions will offer both petrol and diesel engines with a 2.0-liter capacity, delivering 204 hp (340 Nm) and 204 hp (400 Nm), paired with a seven-speed dual-clutch transmission. The powerful 3.0-liter V6 in the Audi SQ5 boasts an impressive 376 hp and 550 Nm of torque. 🗓️Availability: The new Q5 will hit European markets in Q1 2025, with pre-orders already open, starting from €52,300 to €82,900. ▎🔥 Get ready to experience luxury like never before! #AudiQ5#NewGeneration#LuxurySUV#Audi#Innovation#MHEV#Auto