re模組補充和常用內建模組
阿新 • • 發佈:2022-03-29
re模組補充
findall與分組
findall預設是分組優先展示。正則表示式中如果有括號分組,預設只展示括號內正則表示式匹配到的內容
也可以用(?:)取消分組優先展示的機制
import re res = re.findall('n(o) ', "The wise man builds no hopes for the future, entertains no regrets for the past") print(res) # ['o', 'o'] res = re.findall('n(?:o) ', "The wise man builds no hopes for the future, entertains no regrets for the past") print(res) # ['no ', 'no '] res = re.findall('(n)(o)( )', "The wise man builds no hopes for the future, entertains no regrets for the past") print(res) # [('n', 'o', ' '), ('n', 'o', ' ')] res = re.findall('(?P<name1>n)(o)( )', "The wise man builds no hopes for the future, entertains no regrets for the past") print(res) # [('n', 'o', ' '), ('n', 'o', ' ')] 起別名沒有意義,不支援group呼叫 # print(res.group('name1')) # 'list' object has no attribute 'group'
search與分組
# 針對search有多少個分組,group方法括號內參數最大就是多少 res = re.search('n(o) ', "The wise man builds no hopes for the future, entertains no regrets for the past") print(res.group()) # no print(res.group(0)) # no print(res.group(1)) # o res = re.search('n(o)( )', "The wise man builds no hopes for the future, entertains no regrets for the past") print(res.group()) # no print(res.group(0)) # no print(res.group(1)) # o print(res.group(2)) # 空格 # 分組之後還可以給組起別名 res = re.search('(?P<name1>n)(?P<name2>o)( )', "The wise man builds no hopes for the future, entertains no regrets for the past") print(res.group('name1')) # n print(res.group('name2')) # o
match與分組
# 針對match有多少個分組,group方法括號內參數最大就是多少 res = re.match('(T)he', "The wise man builds no hopes for the future, entertains no regrets for the past") print(res.group()) # The print(res.group(0)) # The print(res.group(1)) # T res = re.match('(T)(h)e', "The wise man builds no hopes for the future, entertains no regrets for the past") print(res.group()) # The print(res.group(0)) # The print(res.group(1)) # T print(res.group(2)) # h # 分組之後還可以給組起別名 res = re.match('(?P<name1>T)(?P<name2>h)(e)', "The wise man builds no hopes for the future, entertains no regrets for the past") print(res.group('name1')) # T print(res.group('name2')) # h
常用內建模組
collections模組
具名元組
from collections import namedtuple
# 第一步、先產生一個元組物件模板
point = namedtuple('商品', ['product_name', 'product_price'])
# 第二步、建立諸多元組資料
product_info = point('pen', '$50')
product_info1 = point('book', '$20')
product_info2 = point('desk', '$100')
print(product_info, product_info1, product_info2) # 商品(product_name='pen', product_price='$50') 商品(product_name='book', product_price='$20') 商品(product_name='desk', product_price='$100')
print(product_info.product_name) # pen
print(product_info.product_price) # $50
print(product_info2.product_name) # desk
print(product_info2.product_price) # $100
雙端佇列
# 普通佇列
import queue
name_info = queue.Queue(5) # 最大隻能放三個元素
# 存放元素
name_info.put('jack')
name_info.put('tom')
name_info.put('annie')
name_info.put('sam')
name_info.put('mary')
# name_info.put('jerry') # 佇列以滿,原地等待
# 獲取元素
print(name_info.get()) # jack
print(name_info.get()) # tom
print(name_info.get()) # annie
print(name_info.get()) # sam
print(name_info.get()) # mary
# print(name_info.get()) # 佇列以空,原地等待
# 雙端佇列
from collections import deque
name_info = deque(['jack', 'tom', 'annie'])
print(name_info) # deque(['jack', 'tom', 'annie'])
name_info.append('sam') # 右邊新增元素
print(name_info) # deque(['jack', 'tom', 'annie', 'sam'])
name_info.appendleft('mary') # 左邊新增元素
print(name_info) # deque(['mary', 'jack', 'tom', 'annie', 'sam'])
name_info.pop() # 右邊彈出元素
name_info.popleft() # 左邊彈出元素
print(name_info) # deque(['jack', 'tom', 'annie'])
字典相關
# 正常的字典內部是無序的
d1 = dict([('name', 'sam'), ('age', 20), ('hobby', 'play games'), ('height', '175cm')])
for i in d1:
print(i) # name age hobby height
print(d1) # {'name': 'sam', 'age': 20, 'hobby': 'play games', 'height': '175cm'}
print(d1.keys()) # dict_keys(['name', 'age', 'hobby', 'height'])
# 有序字典
from collections import OrderedDict
d2 = OrderedDict([('a', 97), ('b', 98), ('c', 99)])
print(d2) # OrderedDict([('a', 97), ('b', 98), ('c', 99)])
print(chr(102)) # f
d2['d'] = 100
d2['e'] = 101
d2['f'] = 102
print(d2) # OrderedDict([('a', 97), ('b', 98), ('c', 99), ('d', 100), ('e', 101), ('f', 102)])
print(d2.keys()) # odict_keys(['a', 'b', 'c', 'd', 'e', 'f'])
將列表中[11, 22, 33, 44, 55, 66, 77, 88, 99, 111, 222, 333, 444, 555, 666]大於111的值儲存到字典的第一個key中,將不大於111的值儲存至第二個key的值中。
l1 = [11, 22, 33, 44, 55, 66, 77, 88, 99, 111, 222, 333, 444, 555, 666]
dict1 = {'k1': [], 'k2': []}
for i in l1:
if i > 111:
dict1['k1'].append(i)
else:
dict1['k2'].append(i)
print(dict1) # {'k1': [222, 333, 444, 555, 666], 'k2': [11, 22, 33, 44, 55, 66, 77, 88, 99, 111]}
from collections import defaultdict
l2 = [11, 22, 33, 44, 55, 66, 77, 88, 99, 111, 222, 333, 444, 555, 666]
dict2 = defaultdict(list)
for value in l2:
if value > 111:
dict2['k1'].append(value)
else:
dict2['k2'].append(value)
print(dict2) # defaultdict(<class 'list'>, {'k2': [11, 22, 33, 44, 55, 66, 77, 88, 99, 111], 'k1': [222, 333, 444, 555, 666]})
計數器
string = 'aasdhjbhsBHjababbcbdabbbahabbcajjcbahdhbchccajjcaajcahchahccaasbb'
# 統計字串中所有字元出現的次數
dict1 = {}
for i in string:
if i not in dict1:
dict1[i] = 1
else:
dict1[i] += 1
print(dict1) # {'a': 16, 's': 3, 'd': 3, 'h': 9, 'j': 7, 'b': 14, 'B': 1, 'H': 1, 'c': 11}
from collections import Counter
res = Counter(string)
print(res) # Counter({'a': 16, 'b': 14, 'c': 11, 'h': 9, 'j': 7, 's': 3, 'd': 3, 'B': 1, 'H': 1})
print(res.get('a')) # 16
print(res.get('b')) # 14
print(res.get('j')) # 7