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

Пребарај: #convertible

当前筛选 #convertible清除筛选
Car News

@car_news · Post #618 · 09.10.2024 г., 17:35

🚘 has presented modernized coupe and convertible LC The model range, produced since 2017, got slightly modernized exterior and new interior materials, but at the same time received a more rigid body. Additional struts installed under the floor in the area of the front and rear axles should favorably affect the handling. The Lexus LC engine range consists of a five-liter V8 with 477 horsepower or a 3.5-liter V6-based hybrid unit with 359 hp. Starting from $107,625. @CarsNews #Lexus#hybrid#convertible

Auto Life

@revheadcrypto · Post #298 · 10.08.2024 г., 12:16

🚗✨ Exciting News from Mercedes-Maybach! ✨🚗 Get ready for the debut of the stunning new convertible on August 15th at the prestigious Pebble Beach! 🌊🏖️ The highly anticipated model has been teased since 2022, and we can't wait to see it in all its glory! 🤩 🔍 Fuel Efficiency: The Mercedes-Maybach SL boasts an impressive fuel consumption of 13.7 liters per 100 km*, closely matching the non-hybrid Mercedes-AMG SL 63 with its powerful 4.0L V8 engine, delivering a thrilling 585 hp and 800 Nm* of torque! 💨🔥 #MercedesMaybach#Convertible#LuxuryCars#PebbleBeach#CarDebut#FuelEfficiency#AMG#V8Power#Auto