当前位置:网站首页>Pampy | powerful pattern matching tool
Pampy | powerful pattern matching tool
2022-06-10 04:20:00 【Python Institute】

What is pattern matching
Pattern matching is given a pattern , Use this pattern to check whether the sequence or string conforms to this pattern , This technology is often used in natural language processing .
download pampy
pip install pampy
chestnuts
Single character matching
The following code can complete the matching of a single character in the object , Use _ Indicates the matching result .
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))
Execution results :
>>> python test.py
>>> result is: 4
Match the beginning and the end
For objects that start or end consecutively , We can use this method to achieve fast matching .
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))
Execution results :
>>> python test.py
>>> {'head': 'a', 'body': 1, 'tail': ['d', 4]}
above , We use HEAD Matches the first few characters , In the middle _ Matched a number , At the end we use TAIL With several characters .
Matching dictionary key
When we only know part of a dictionary , But want to get something value Of key when , Get twice the result with half the effort in this way .
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))
Running results :
>>> python test.py
>>> {'key1': 'phone', 'key2': 'huawei'}
Above , We have matched the first and second layers of the dictionary Key value .
The example above , Our model must keep the dictionary structure intact .
End
Previous recommendation
MeterSphere | Super easy to use open source testing platform
Doctest | Super simple unit test tool
9 Methods | Improve domestic visits GitHub The speed of
FastApi-01- First time to know
FastApi-07- Query parameter verification
FastApi-08- Path parameter verification
FastApi-11- Template rendering
The long screenshot of the web page is so simple
Python-10- File read and write
100 individual Python example , From entry to burial !
The article looks better here

边栏推荐
- Today, 19:30 | graphics special session - Gao Lin's team from Institute of computing technology, Chinese Academy of Sciences
- Perform tests
- Descriptor ready condition
- 这些编程语言老了,差不多死透了,年轻人不能碰
- 电阻电容封装选型经验详解
- 源代码加密软件类型分析
- Use 80% of the charts to meet daily work, and use the remaining 20% of the charts to build core competitiveness!
- 多商户商城小程序源码有何优势?
- [激光器原理与应用-1]:什么是激光器以及激光器的常见应用
- [深入研究4G/5G/6G专题-29]: 5G NR开机流程5.1 - NR网络架构、基站的核心网相关配置
猜你喜欢

分布式数据对象:超级终端的'全局变量'

Gevent | 异步就用它!

As a software testing engineer, give advice to young people (Part 1)

CVPR 2022 | indirect lighting modeling in inverse rendering

Why do SQL statements use indexes but still slow queries?

Unity光照黑莫名其妙的偏色问题

Leetcode weekly buckle race 296

Golang learning 6: file operation in

Qpprogressbar+qpushbutton+qmainwindow+qtmer+ layout manager +qtextcodec+qtexttospeech in QT
![[adaptive motion compensation] FPGA based adaptive motion compensation video image enhancement system](/img/7b/a9073815347265c641b146bd30e1ba.png)
[adaptive motion compensation] FPGA based adaptive motion compensation video image enhancement system
随机推荐
Final examination paper 2 of the first postgraduate course in Dialectics of nature
[深入研究4G/5G/6G专题-23]: 5G NR开机流程4.1 - 随机接入请求消息MSG1与PRACH首个上行信道的调度、时间提前量TA的检测
[Error] anonymous type with no linkage used to declare function ‘bool InitSLinkList
[深入研究4G/5G/6G专题-29]: 5G NR开机流程5.1 - NR网络架构、基站的核心网相关配置
[understanding of opportunity -22]: Guiguzi - the art of closing Tibet. Collect your talents in time to protect yourself in the workplace, business and business activities
90. locking
91. 栅栏
What do you usually ask about "ALI / byte" automation test interview questions in large factories, as well as the skills and answers
[enlightenment -18]: Confucianism, Buddhism and Taoism, different life stages, different choices
Common database field type mapping relationships
I wrote getter and setter methods every day. I was impatient, so I used the artifact mapperstruct, and the crud efficiency was suddenly improved! (hard coding is preferred)
Cross in tensorflow_ entropy
[从零开始学习FPGA编程-12]: 快速入门篇 - 操作步骤3(功能仿真)-1-Mentor HDL仿真工具modelsim SE 10.4简介与安装
epoll的两种模式和epoll反应堆
源代码加密软件类型分析
Encrypting CEPH RBD using luks2
[机缘参悟-20]:鬼谷子-捭阖篇-一开一合,一动一静,一张一弛谓之捭阖,职场应用。
Pytoch cpu/gpu installation method.
[机缘参悟-19]:鬼谷子-捭阖篇-阴阳即矛盾,是万事万物的基本属性,也是鬼谷子哲学的根基之一
[Android L]SEAndroid增强Androd安全性背景概要及带来的影响