当前位置:网站首页>Pampy | 超强的模式匹配工具
Pampy | 超强的模式匹配工具
2022-06-10 04:11:00 【Python研究所】

何为模式匹配
模式匹配即给定某种模式,用这种模式去检查序列或字符串是否符合这种模式,这种技术在自然语言处理中经常使用。
下载pampy
pip install pampy
栗子
单个字符匹配
以下代码可以完成单个字符在对象中的匹配,使用_表示匹配结果。
from pampy import _,match
a=['a',1,'b',2,'c',3,'d',4]
patter = ['a',1,'b',2,'c',3,'d',_]
action=lambda x: f'result is: {x}'
print(match(a,patter,action))
执行结果:
>>> python test.py
>>> result is: 4
匹配开头和结尾
对于开头或者结尾连续的对象,我们可以使用这种方式实现快速匹配。
from pampy import _,match,HEAD,TAIL
a=['a',1,'b',2,'c',3,'d',4]
patter = [HEAD,_,'b',2,'c',3,TAIL]
action=lambda h,b,t: ({'head':h,'body':b,'tail':t})
print(match(a,patter,action))
执行结果:
>>> python test.py
>>> {'head': 'a', 'body': 1, 'tail': ['d', 4]}
以上,我们使用HEAD匹配了开头的若干字符,中间使用_匹配了某个数字,结尾我们使用TAIL配了若干字符。
匹配字典的key
当我们只知道某个字典的部分内容,却想要得到某个value的key时,用这种方式事半功倍。
from pampy import _,match,HEAD,TAIL
my_dic={
'phone':{'huawei':'ok','iphone':'good','chuizi':'bad'},
'language':{
'chinese':['xian','beijing'],
'english':['usa','canada']
}
}
patter = {_:{_:'ok'}}
action=lambda a,b: {'key1':a,'key2':b}
print(match(my_dic,patter,action))
运行结果:
>>> python test.py
>>> {'key1': 'phone', 'key2': 'huawei'}
如上,我们已经匹配到了字典的第一层和第二层的Key值。
如上面的例子,我们的模式一定要保持字典结构的完整。
完
往期推荐
文章好看点这里

边栏推荐
- MySQL - Installation
- SSTI (template injection) - (7)
- [special introduction] round table forum -- the integration of AI and audio and video technology
- 91. fence
- Golang learning 5: error, painc, recover
- Storage engine of MySQL database
- 1-浏览器插件
- [semi supervised classification] semi supervised web page classification based on K-means and label+propagation
- Gevent | 异步就用它!
- MySQL learning
猜你喜欢

MySQL learning

LeetCode 力扣周赛 296

Good news 𞓜 wangchain technology signed the Miluo cultural, tourism and sports industry project to create a digital village on the "chain"

Redisson YML configuration error

在线文本字符串批量替换工具

Comment écrire du Code Scala dans idea

QT中的QProgressBar+QPushButton+QMainwindow+QTmer+布局管理器+QTextCodec+QTextToSpeech

Open vs code from current folder
![[enlightenment -18]: Confucianism, Buddhism and Taoism, different life stages, different choices](/img/3c/68eec2d6e29b703d2182b4cbba91e1.png)
[enlightenment -18]: Confucianism, Buddhism and Taoism, different life stages, different choices

用80%的图表满足日常工作,用剩下20%的图表建立核心竞争力!
随机推荐
[learn FPGA programming from scratch -12]: quick start chapter - operation step 3 (functional simulation) -1-mentor HDL simulation tool Modelsim se 10.4 introduction and installation
[semi supervised classification] semi supervised web page classification based on K-means and label+propagation
“阿里/字节“大厂自动化测试面试题一般会问什么?以及技巧和答案
[learn FPGA programming from scratch -14]: quick start chapter - operation step 3 (functional simulation) -3-modelsim quick start (8bits cycle counter)
[机缘参悟-18]:儒释道,不同人生阶段,不同选择
分布式事务解决方案
Find and replace good articles from the Internet in vim/vi
5- common tool management
[机缘参悟-20]:鬼谷子-反应篇-动静之术、说听结合、沉默是金
在Vim/Vi中查找和替换,从网上遇到的好文章
[科技特长-1]:科技特长生概述与优势
源代码加密软件类型分析
[从零开始学习FPGA编程-14]:快速入门篇 - 操作步骤3(功能仿真)-3-modelsim快速使用入门(8bits循环计数器)
Why use layer 3 switches
在线文本字符串批量替换工具
[image fusion] Application of two-dimensional multi empirical mode decomposition in multi-scale image fusion with matlab code
Google Earth engine (GEE) - gpwv411: data set of average administrative unit area
Award winning research
Redisson YML configuration error
每天都在写Getter、Setter方法,我不耐烦了,于是用了神器MapperStruct,crud效率一下子提高了! (最好使用硬编码)