原文链接:https://www.wolfram.com/language/fast-introduction-for-programmers/en/patterns/
正文
Patterns stand for classes of expressions.
模式代表着表达式的种类。
The basic pattern construct _ (pronounced “blank”) stands for any expression.
最基本的模式结构是_,其代表任何表达式。
Find cases in a list matching the pattern x^_:
在一个列表中找到匹配x^的模式
![Mathematica入门[8]–Patterns(模式)](https://img.mathpretty.com/snipaste_20170925_195015.png?imageView2/0/q/75|watermark/2/text/5YWz5rOo5YWs5LyX5Y-377ya5paH6Im65pWw5a2m5ZCb/font/5b6u6L2v6ZuF6buR/fontsize/540/fill/IzAwMDAwMA==/dissolve/100/gravity/SouthEast/dx/10/dy/10|imageslim)
x (short for x:) stands for a pattern whose value will be named x:
x_是x:_的缩写,代表一个模式,其值将会被命名为x
(“double blank”) stands for any sequence of expressions:
(双_)代表任何表达式序列(一个或多个表达式,不能为0)
![Mathematica入门[8]–Patterns(模式)](https://img.mathpretty.com/snipaste_20170925_202621.png?imageView2/0/q/75|watermark/2/text/5YWz5rOo5YWs5LyX5Y-377ya5paH6Im65pWw5a2m5ZCb/font/5b6u6L2v6ZuF6buR/fontsize/540/fill/IzAwMDAwMA==/dissolve/100/gravity/SouthEast/dx/10/dy/10|imageslim)
/. means “replace everywhere”:
/.意味着全部替换
![Mathematica入门[8]–Patterns(模式)](https://img.mathpretty.com/snipaste_20170925_201356.png?imageView2/0/q/75|watermark/2/text/5YWz5rOo5YWs5LyX5Y-377ya5paH6Im65pWw5a2m5ZCb/font/5b6u6L2v6ZuF6buR/fontsize/540/fill/IzAwMDAwMA==/dissolve/100/gravity/SouthEast/dx/10/dy/10|imageslim)
a | b | c stands for a, b, or c:
a | b | c代表a或者b或者c
![Mathematica入门[8]–Patterns(模式)](https://img.mathpretty.com/snipaste_20170925_201304.png?imageView2/0/q/75|watermark/2/text/5YWz5rOo5YWs5LyX5Y-377ya5paH6Im65pWw5a2m5ZCb/font/5b6u6L2v6ZuF6buR/fontsize/540/fill/IzAwMDAwMA==/dissolve/100/gravity/SouthEast/dx/10/dy/10|imageslim)
_h stands for any expression with head h:
_h代表任何头部为h的表达式
![Mathematica入门[8]–Patterns(模式)](https://img.mathpretty.com/snipaste_20170925_201804.png?imageView2/0/q/75|watermark/2/text/5YWz5rOo5YWs5LyX5Y-377ya5paH6Im65pWw5a2m5ZCb/font/5b6u6L2v6ZuF6buR/fontsize/540/fill/IzAwMDAwMA==/dissolve/100/gravity/SouthEast/dx/10/dy/10|imageslim)
- :> is a delayed rule—the analog of := for a rule.
- :> 是一个延迟规则—类似于延迟赋值 := .
在python中使用replace进行替换
str = ['x^2','x^3','x^4']
[i.replace('x','y') for i in str]
![Mathematica入门[8]–Patterns(模式)](https://img.mathpretty.com/snipaste_20180101_215735.png)
在python中的模式匹配,使用re模块
str = ['x^2', 'x^3', 'x^4', 'a^2', 'b^2', 'y^2']
[re.findall(r'x\^[\d]|y\^[\d]',i) for i in str]
![Mathematica入门[8]–Patterns(模式)](https://img.mathpretty.com/snipaste_20180101_222056.png)
一些句子及单词
sequence:序列
analog:模拟的
- 微信公众号
- 关注微信公众号
-
- QQ群
- 我们的QQ群号
-


![Mathematica入门[13]–Associations(关联)](https://img.mathpretty.com/20210911_162059_8b2hciq.jpg)
![Mathematica入门[12]–Options(选项)](https://img.mathpretty.com/20210911_162132_3tfm36s.jpg)
![Mathematica入门[11]–Applying Functions(函数应用)](https://img.mathpretty.com/20210911_162025_0gwx5df.jpg)
![Mathematica入门[10]–Pure Functions(纯函数)](https://img.mathpretty.com/20210911_161424_vlvm1hh.jpg)
![Mathematica入门[9]–Function Definitions(函数定义)](https://img.mathpretty.com/20210911_162114_futklns.jpg)
![Mathematica入门[7]--Assignments(赋值)](https://img.mathpretty.com/20210911_162124_pk73t7w.jpg)
![Mathematica入门[6]--Iterators(迭代器)](https://img.mathpretty.com/20210911_162127_srsayfn.jpg)
![Mathematica入门[5]--Lists(列表)](https://img.mathpretty.com/20210911_161401_9q0dovo.jpg)
![Mathematica入门[4]--Symbolic Expressions(符号表达式)](https://img.mathpretty.com/20210911_162108_oqvo4xz.jpg)

评论