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

Пребарај: #ferrarif80

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

@revheadcrypto · Post #589 · 19.10.2024 г., 08:26

🚗💨Ferrari Unveils the F80: A New Super Hybrid Rivaling McLaren W1!💨🚗 Introducing the most powerful road model in Ferrari's history! 🔥🏎️ 🔴 Instead of a V12, it features a Le Mans-inspired V6 3.0 with hybrid technology. 🔴 Produces a staggering 900 hp, revving up to 9000 RPM! 🔴 Combined output with electric motors (developed by Ferrari) reaches an astonishing 1200 hp! 🔴 0-100 km/h in just 2.15 seconds! 🔴 0-200 km/h in only 5.75 seconds! 🔴 Top speed of 350 km/h! 🔴 800-volt electrical architecture for ultimate performance! 🔴 Dry weight of 1525 kg! 🔴 Monocoque made from carbon fiber, with aluminum front and rear subframes! The F80 features a two-seat configuration, with a driver-focused cockpit, dubbed "1+." 🏁👤 Its retro design pays homage to the legendary Daytona and F40, generating a whopping ton of downforce at 250 km/h! 🌬️✨ 🚀Availability: Late 2025 💰Starting Price: €3.6 million 🔢 Only 799 units will be produced! #FerrariF80#SuperHybrid#Hypercar#Ferrari#McLarenW1#CarLovers#Auto