当前位置:网站首页>Derivation and regularization
Derivation and regularization
2022-04-23 10:41:00 【qq1033930618】
Derivation and regularization
One 、 The derived type
res = []
for i in range(1, 11):
res.append(i ** 2)
print(res) # [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
res = [x ** 2 for x in range(1, 11)] # The outer square brackets indicate the list derivation
print(res)
res = []
for i in range(1, 11):
if i ** 2 % 2 == 0:
res.append(i ** 2)
print(res) # [4, 16, 36, 64, 100]
res = [x ** 2 for x in range(1, 11) if x ** 2 % 2 == 0]
print(res)
res = [x for x in "python"] # Split string
print(res) # ['p', 'y', 't', 'h', 'o', 'n']
res = [x + y for x in "python" for y in "123" if x + y != "y1"] # Split string
print(res)
# ['p1', 'p2', 'p3', 'y2', 'y3', 't1', 't2', 't3', 'h1', 'h2', 'h3', 'o1', 'o2', 'o3', 'n1', 'n2', 'n3']
res = (x ** 2 for x in range(1, 11)) # () Is defined as a generator
print(res) # <generator object <genexpr> at 0x7fdb743e9570>
res = tuple(x ** 2 for x in range(1, 11)) # keyword tuple Deducing tuples
print(res)
dic = {
x: x ** 2 for x in range(1, 11)} # {} Brace Dictionary With colon :
print(dic)
ss = {
x for x in range(1, 11)} # Unordered and unrepeatable sets No colon
print(ss)
ss = {
x for x in "aaa123"} # Unordered and unrepeatable sets No colon
print(ss)
Two 、 Regular form
import re
# Regular expressions character string Focus on handling rules
result = re.findall("py", "python") # "python" Whether contains py
print(result)
result = re.findall("pn", "python") # "python" Whether contains pn
print(result)
result = re.findall("python", "I like python") # Whether the sentence contains words
print(result)
result = re.findall("o", "I love python") # Whether the sentence contains letters
print(result)
result = re.findall("2", "1234567890abcdef") # Whether the sentence contains numbers
print(result)
# Predefined characters
# \d \s \w \D \S \W Case complements each other
# \d All figures 0123456789
# \D The digital
# \w Underline included a-z A-Z 0-9
# \W Special characters Underline without numbers, uppercase and lowercase letters
# \s blank Tabulation Line break
# \S The blank Tabulation Line break
result = re.findall("\d", "12a789bc345f67890de") # All the numbers in the sentence
print(result)
result = re.findall(r"\d", "12a789bc345f67890de") # All the numbers in the sentence Don't want escape processing to make r Original output
print(result)
result = re.findall(r"\D", "1de2abc345f678__90de") # Not all numbers in the sentence
print(result)
result = re.findall(r"\w", "1de2abc345f6__7890A$%&Ade") # All sentences include underscores a-z A-Z 0-9
print(result)
result = re.findall(r"\W", "1de2abc345f6__7890A$%&Ade") # All special characters in the sentence Underline without numbers, uppercase and lowercase letters
print(result)
result = re.findall(r"\s", "1de2abc 345f\n6__7890A\t$%&Ade") # All blanks in the sentence Tabulation Line break
print(result)
result = re.findall(r"\S", "1de2abc 345f\n6__7890A\t$%&Ade") # All non blanks in the sentence Tabulation Line break
print(result)
# Metacharacters
# [] Match a character Or relationship
result = re.findall(r"[123]", "1e3deabc45f\n6__7890A\t$%&Ade") # 1 or 2 or 3
print(result)
result = re.findall(r"[\d\s]", "1e3deabc45f\n6__7890A\t$%&Ade") # \d or \s
print(result)
result = re.findall(r"[^\d\s]", "1e3deabc45f\n6__7890A\t$%&Ade") # \d or \s Take the opposite ^ Only take the opposite in square brackets
print(result)
# - Section
result = re.findall(r"[1-7]", "1e3deabc45f\n6__7890A\t$%&Ade") # 1 To 7
print(result)
result = re.findall(r"[b-f]", "1e3deaCCCbc45f\n6__7890A\t$%&Ade") # b To f
print(result)
result = re.findall(r"[1-7b-f]", "1e3deabc45f\n6__7890A\t$%&Ade") # 1-7 or b-f All together
print(result)
# All the above concepts are one character
# () On behalf of the group
result = re.findall(r"a([abc])", "aaabacad")
print(result)
result = re.findall(r"a[abc]", "aaabacad") # a It can be followed by any or one of the relationships lookup aa ab ac
print(result)
# Repeat match
result = re.findall(r"\d\d\d", "123456789abcdefg") # Three consecutive numbers in a sentence
print(result)
# {n} The first character is repeated n Time
result = re.findall(r"\d{3}", "123456789abcdefg") # Three consecutive numbers in a sentence
print(result)
# {n, m} The first character repeats at least n Time at most m Time
result = re.findall(r"\d{2,5}", "12e3456789abc6defg") # Continuous in a sentence 2-5 A digital
print(result) # He will match as many as possible Greedy matching
result = re.findall(r"\d{2,5}?", "12e3456789abc6defg") # Continuous in a sentence 2-5 A digital
print(result) # Type an English half angle ? Non greedy matching and {n} No difference between
result = re.findall(r"\d{2,}", "12e3456789abc6defg") # Numbers appear at least twice in a row in a sentence
print(result) # Greedy matching
# ? Leading characters appear 0 or 1 Time {0,1}
result = re.findall(r"\d?", "12e3456789abc6defg")
print(result) # Non numeric places are filled with empty strings " " There will be another end at the end " "
# + The preceding characters appear at least 1 Time {1,}
result = re.findall(r"\d+", "12e3456789abc6defg")
print(result) # greedy Block mixing
result = re.findall(r"\d+?", "12e3456789abc6defg")
print(result) # Not greed The numbers are extracted one by one
# * The preceding character does not appear appear 0 Time Appear any number of times
result = re.findall(r"\d*", "12e3456789abc6defg") # Numbers appear at least twice in a row in a sentence
print(result) # greedy Block mixing And each non numeric character has " " placeholder
# Escape symbol *+?
result = re.findall(r"\*", "12e*345a*b67*89*abc6d*efg") # look for * Must escape
print(result)
result = re.findall(r"a\*b", "12e*345a*b67*89*abc6d*efg") # look for * Must escape
print(result)
result = re.findall(r"a*b", "12e*345a*b67*89*abc6d*efg") # look for b front a It doesn't matter Print if you have
print(result)
result = re.findall(r"\\d", "12e*345\da*b67*\d89*abc6d*efg") # look for "\d"
print(result) # The print should be \\d instead of \d Although I'm looking for \d
# Greed and non greed
result = re.findall(r"d\w+d", "dxxxxxxxxdxxxxxd") # Greedy with d Begin with d ending
print(result) # dxxxxxxxxdxxxxxd
result = re.findall(r"d\w+?d", "dxxxxxxxxdxxxxxd") # Not greedy with d Begin with d ending
print(result) # dxxxxxxxxd
htmlstr = "<td>python</td><td>$123</td><td>[email protected]</td>"
result = re.findall(r"<td>.+</td>", htmlstr) # . Everything except line breaks
print(result) # ['<td>python</td><td>$123</td><td>[email protected]</td>']
htmlstr = "<td>python</td><td>$123</td><td>[email protected]</td>"
result = re.findall(r"<td>.+?</td>", htmlstr) # Not greed
print(result) # ['<td>python</td>', '<td>$123</td>', '<td>[email protected]</td>']
htmlstr = "<td>python</td><td>$123</td><td>[email protected]</td>"
result = re.findall(r"<td>(.+?)</td>", htmlstr) # Just within parentheses
print(result) # ['python', '$123', '[email protected]']
# backreferences
wordstr = """'hello' "python" 'love" "haha'"""
# Extract words with the same symbols on both sides
result = re.findall(r"['\"]\w+['\"]", wordstr) # Just within parentheses If not + It defaults to the middle letter Can't print out
print(result) # ["'hello'", '"python"', '\'love"', '"haha\'']
result = re.findall(r"(['\"])(\w+)(\1)", wordstr) # Front side () Put a group (\1) Is the content of the first group
print(result) # [("'", 'hello', "'"), ('"', 'python', '"')] The matched three parts in parentheses form a tuple
print([x[1] for x in result]) # ['hello', 'python']
# The verification password cannot appear continuously 3 Subnumeral
result = re.findall(r"(\d)(\1{2,})", "12222223") # Match a number Repeat more than twice
print(result) # [('2', '22222')]
print([x[0]+x[1] for x in result]) # ['222222']
# Position matching ^( Not in brackets ) start $ ending
result = re.findall(r"\d{11}", "13578617265123123")
print(result)
result = re.findall(r"^\d{11}", "13578617265123123") # Match from front
print(result)
result = re.findall(r"\d{11}$", "13578617265123123") # Match from behind
print(result)
result = re.findall(r"^\d{11}$", "13578617265123123") # Must happen to 11 Numbers
print(result)
版权声明
本文为[qq1033930618]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230618497612.html
边栏推荐
- A diary of dishes | 238 Product of arrays other than itself
- Linked list intersection (linked list)
- Comparison and practice of prototype design of knowledge service app
- Charles function introduction and use tutorial
- Operation of 2022 tea artist (primary) test question simulation test platform
- 【leetcode】199.二叉树的右视图
- 微信小程序简介、发展史、小程序的优点、申请账号、开发工具、初识wxml文件和wxss文件
- 图像处理——噪声小记
- /Can etc / shadow be cracked?
- Chapter 2 Oracle database in memory architecture (I) (im-2.1)
猜你喜欢

Sim Api User Guide(6)

Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022

SQL Server 递归查询上下级

/Can etc / shadow be cracked?

得到知识服务app原型设计比较与实践

Shell script interaction free

Initial exploration of NVIDIA's latest 3D reconstruction technology instant NGP

JVM - common parameters

Solution architect's small bag - 5 types of architecture diagrams

Notes on concurrent programming of vegetables (V) thread safety and lock solution
随机推荐
景联文科技—专业数据标注公司和智能数据标注平台
中职网络安全2022国赛之CVE-2019-0708漏洞利用
高价买来的课程,公开了!phper资料分享
Sim Api User Guide(7)
Linked list intersection (linked list)
Diary of dishes | Blue Bridge Cup - hexadecimal to octal (hand torn version) with hexadecimal conversion notes
JUC concurrent programming 07 -- is fair lock really fair (source code analysis)
Chapter 2 Oracle database in memory architecture (I) (im-2.1)
Six practices of Windows operating system security attack and defense
MySQL how to merge the same data in the same table
Net start MySQL MySQL service is starting MySQL service failed to start. The service did not report any errors.
19、删除链表的倒数第N个节点(链表)
SQLServer 查询数据库死锁
707. Design linked list (linked list)
Turn: Maugham: reading is a portable refuge
CSP certification 202203-2 travel plan (multiple solutions)
What about Jerry's stack overflow? [chapter]
JVM——》常用命令
IDEA——》每次启动都会Indexing或 scanning files to index
Arm debugging (1): two methods to redirect printf to serial port in keil