@Perse_pol_is · Post #190 · 17.03.2020 г., 14:11
Relief of a #Median tribute bearer presenting an Acinace for the king, excavated in Persepolis.
Hashtags
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
Пребарај: #median
@Perse_pol_is · Post #190 · 17.03.2020 г., 14:11
Relief of a #Median tribute bearer presenting an Acinace for the king, excavated in Persepolis.
Hashtags
@Perse_pol_is · Post #189 · 16.03.2020 г., 09:30
#Median Art. Glazed vase decorated with goats and a lotus flower, 7th century BC, part of the Ziwiyeh hoard.
Hashtags
@Perse_pol_is · Post #147 · 22.02.2020 г., 18:34
#Median art. Gold protoma of a bull, 8th century BC, possibly part of the Ziwiyeh treasure. Currently housed in the national museum of Warsaw.
Hashtags
@Perse_pol_is · Post #128 · 04.02.2020 г., 17:29
"The Medes were formerly called by everyone Aryans, but when the Colchian woman Medea came from Athens to the Aryans they changed their name, like the Persians. This is the Medes' own account of themselves. " —Herodotus, The Histories, Book 7, Chapter 62. [Picture]: Bust of a #Median lion strangler, 5th century BC.
Hashtags
@Perse_pol_is · Post #123 · 03.02.2020 г., 17:30
A 6th century BC silver rython depicting a #Median horserider, with his people's distinctive round hat. Excavated in Erebuni, Urartian "fortress of blood" which was occupied by the Medians and the Persians.
Hashtags
@Perse_pol_is · Post #122 · 03.02.2020 г., 17:24
Gold rython in shape of a ram's head, excellent example of #Median art, part of the Ziwiyeh treasure.
Hashtags
@Perse_pol_is · Post #148 · 22.02.2020 г., 19:04
Bas-relief of a #Persian (right) and #Median (left) lancer, Persepolis.