Небольшой трик с регулярными выражениями который редко вижу в чужом коде.
Допустим, вам нужно распарсить простой текст и вытащить оттуда пары имя+телефон. Вернуть всё это надо в виде списка словарей. Возьмем очень простой пример текста.
>>> 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
#writing
@ieltsstrategies
🖌📕Make the most of your Writing test:
@ieltsstrategies
• analyse each task properly and spend some time making notes
• highlight or underline key words in the tasks to make sure that you focus on what you have to do
• plan your answers
• use paragraphs clearly; put one idea in each paragraph
• do not repeat ideas using different words
• do not copy whole sentences from the question – you will receive no marks for this
• keep to the topic; do not write about unrelated subjects
• manage your time; remember, Task 2 is worth twice as much as Task 1
• spend approximately 20 minutes on Task 1 and approximately 40 minutes on Task 2
• you must write your answers in full; answers written in note form or in bullet points will lose marks
@ieltsstrategies
• pay attention to spelling, grammar and punctuation; you will lose marks for mistakes
• avoid informal language
• do not memorise model answers; examiners are trained to recognise them and your test will be invalid
• spend several minutes re-reading and correcting your answers
@ieltsstrategies
#writing
✅ Writing time
🔰 Task 1 (Pie charts 🧇 )
✌️ 2-qism
🧇 Pie charts yozish uchun bu ajoyib qo'llanma o'z ichiga turli foydali mashq va so'zlarni olgan bo'lib foydali bo'ladi degan umiddaman.
♻️@cambridgeIELTSbooks
#writing
✅ Writing time
🔰 Task 1
☺️Sizlarga qanday qilib Task 1 savolini Paraphrase qilishni va shu asosida introduction yozishni o'rgataman. Agar foydali deb hisoblasangiz,ulashing.
♻️@cambridgeIELTSbooks
#writing
✅ Writing time
🔰 Task 1 (Line graph 📉📈)
✌️2-qism
📈 Line graph yozish uchun bu ajoyib qo'llanma o'z ichiga turli foydali mashq va so'zlardan iborat bo'lib foydali bo'ladi degan umiddaman.
♻️@cambridgeIELTSbooks
#writing
Qani endi mana bu task 1 uchun tayyorlagan kichik qo’llanmani ilingchi. Ha evaziga izohlarda “ rahmat “ deb yozib qoldirish “tekin” aytib qo’yay😁. Oldingi postda qoldiribsizlarku, shundan ilhomlanib “tishimni kavagida” saqlayotganlarimni ham ulashyapman aytib qo’yay🤪.
👍@cambridgeieltsbooks
Ayrimlar hatto like bosish pullik deb o’ylaydimi deymanda “reaction”lar kamligini ko’rib ba’zan.
✅Writing time
This book is written by a certified professional IELTS instructor who scored Band 9.0 in Academic IELTS.
It provides useful tips and academic word lists for IELTS writing Task 1.
Sharing is caring🔥✊
#writing
✅Channel: @cambridgeIELTSbooks
✅Writing time
▶️TYPES OF IELTS WRITING TASK-2 QUESTIONS
1️⃣Discuss both views and give your opinion?
2️⃣Do you think the advantages outweigh disadvantages?
3️⃣What are the advantages and disadvantages?
4️⃣Is it a positive or a negative development?
5️⃣What are the problems/reasons/causes for this? What would you suggest as a solution?
6️⃣What are the reasons/causes? And how it affects? /what are the effects? /consequences?
7️⃣What are the reasons for this change? Is it a positive or a negative development?
8️⃣To what extent do you agree or disagree?
9️⃣How far do you agree or disagree?
1️⃣0️⃣Do you agree or disagree?
❗️Please note that serial numbers 8, 9 & 10 have the same essay structure
#writing
♻️Sharing is caring🤓
✅Channel: @cambridgeIELTSbooks
✅IELTS WRITING
▶️In this mini-book you will know types of writing task 1 and academic vocabulary to use.
#writing
🤓Kanalga qo'shiling 👇
⭐️@cambridgeIELTSbooks
YouTube|Instagram|Telegram
3 本写作书籍
https://x.com/lennysan/status/1918418517063512088
Lenny Rachitsky 最近推荐了对他写作帮助最大的 3 本书籍,On Writing Well、Nobody wants to read your sh*t、Several short sentences about writing。
这种可以直接 Deep Research 过一遍,感兴趣的可以选择性阅读,前面我们也分享了很多写作合集,参考、精进。
相关链接
关于如何写作丨主题分享
#writing