re 妯″潡銆乼yping 妯″潡銆乧ollections 妯″潡
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了re 妯″潡銆乼yping 妯″潡銆乧ollections 妯″潡相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e4%b8%8b%e5%88%92%e7%ba%bf' title='涓嬪垝绾?>涓嬪垝绾?/a> pen find 渚嬮 counter iterator 鎻掑叆 UNC
鐩綍
- 涓€銆乺e 妯″潡
- 浜屻€乼yping 妯″潡
- 涓夈€乧ollections 妯″潡
- 1銆佸鍏ユ柟寮?/a>
- 2銆佷綔鐢?/a>
- 3銆佹柟娉?/a>
- 3.1 鏈夊悕鍏冪粍 namedtuple
- 3.2 榛樿瀛楀吀 defaultdict
- 3.3 鍙岀闃熷垪
- 涓€銆乺e 妯″潡
1銆佸鍏ユ柟寮?/h2>
import re
2銆佷綔鐢?/h2>
浠庡瓧绗︿覆閲屾壘鐗瑰畾鐨勫瓧绗︿覆
3銆佸熀鏈娉?/h2>
^
鍖归厤寮€澶?/p>s = 'abdhgtsab' print(re.findall('^abd',s)) # ['abd'] 寮€澶存湁灏辫緭鍑篴bd锛屾病鏈夊氨鍥炶繑鍥瀃]
$ 鍖归厤缁撳熬
s = 'abdhgtsab' print(re.findall('ab$',s)) # ['ab'] 缁撳熬鏈夊氨杩斿洖ab锛屾病鏈夊氨杩斿洖[]
[]
鍖归厤[]鍐呯殑瀛楃s = 'acefghjkacefsdfsdf' print(re.findall('[acef]', s)) # 鍙[]鍐呯殑鍗曚釜瀛楃 ------------------------------------------------------------- ['a', 'c', 'e', 'f', 'a', 'c', 'e', 'f', 'f', 'f']
^[]
瀵筟]閲岄潰鐨勫厓绱犲彇鍙?鍙栧嚭闄や簡[]鍐呯殑鍏冪礌澶栫殑姣忎釜瀛楃s = 'acefghjkacefsdfsdf' print(re.findall('[^acef]', s)) ------------------------------------------------------ ['g', 'h', 'j', 'k', 's', 'd', 's', 'd']
.
浠绘剰瀛楃锛堥櫎浜哱n锛?/p>
s = 'acefghjkacefsdfsdf' print(re.findall('a..', s)) ------------------------------------------------------ ['aba', 'ada']
*
鍓嶉潰鐨勫瓧绗?-鏃犵┓涓?/li>
s = 'abaacaaaaa' print(re.findall('a*', s)) ------------------------------------------------------ ['a', '', 'aa', '', 'aaaaa', '']
+
鍓嶉潰鐨勫瓧绗?-鏃犵┓涓?/li>
s = 'abaacaaaaa' print(re.findall('a+', s)) ------------------------------------------------------ ['a', 'aa', 'aaaaa']
?
鍓嶉潰鐨勫瓧绗?-1涓?/li>
s = 'abaacaaaaa' print(re.findall('a?', s)) ------------------------------------------------------ ['a', '', 'a', 'a', '', 'a', 'a', 'a', 'a', 'a', '']
m
鍓嶉潰鐨勫瓧绗涓?/li>
s = 'abaacaaaaa' print(re.findall('a5', s)) ------------------------------------------------------ ['aaaaa']
m,n
鍓嶉潰鐨勫瓧绗-n涓?/li>
s = 'abaacaaaaa' print(re.findall('a2,5', s)) ------------------------------------------------------ ['aa', 'aaaaa']
\d
鏁板瓧
s = 's 1 s+\n=$\t2_s 3' print(re.findall('\d', s) ------------------------------------------------------ ['1', '2', '3']
\D
闈炴暟瀛?/li>
s = 's 1 s+\n=$\t2_s 3' print(re.findall('\D', s) ------------------------------------------------------ ['s', ' ', ' ', ' ', ' ', ' ', 's', '+', '\n', '=', '$', '\t', '_', 's', ' ', ' ']
\w
鏁板瓧/瀛楁瘝/涓嬪垝绾?/li>
s = 's 1 s+\n=$\t2_s 3' print(re.findall('\w', s)) ------------------------------------------------------ ['s', '1', 's', '2', '_', 's', '3']
\W
闈炴暟瀛?瀛楁瘝/涓嬪垝绾?/li>
s = 's 1 s+\n=$\t2_s 3' print(re.findall('\W', s)) ------------------------------------------------------ [' ', ' ', ' ', ' ', ' ', '+', '\n', '=', '$', '\t', ' ', ' ']
\s
绌烘牸/\t/\n
s = 's 1 s+\n=$\t2_s 3' print(re.findall('\s', s)) ------------------------------------------------------ [' ', ' ', ' ', ' ', ' ', '\n', '\t', ' ', ' ']
\S
闈炵┖鏍?\t/\n
s = 's 1 s+\n=$\t2_s 3' print(re.findall('\S', s)) ------------------------------------------------------ ['s', '1', 's', '+', '=', '$', '2', '_', 's', '3']
\
鍙栨秷鎰忎箟
s = 'aba\d' print(re.findall(r'a\\d', s)) ------------------------------------------------------ ['a\\d']
.*
璐┆妯″紡(鏈€澶у寲),鎵惧埌缁х画鎵?璁╃粨鏋滄渶澶у寲
s = 'abbbcabc' print(re.findall('a.*c', s)) ------------------------------------------------------ ['abbbcabc']
.*?
闈炶椽濠ā寮?鏈€灏忓寲),鎵惧埌灏遍┈涓婂仠姝?/li>
s = 'abbbcabc' print(re.findall('a.*?c', s)) ------------------------------------------------------ ['abbbc', 'abc']
()
鍙鎷彿鍐呯殑
s = 'abacad' print(re.findall('a(.)', s)) ------------------------------------------------------ ['b', 'c', 'd']
A|B
A鍜孊閮借
s = 'abacad' print(re.findall('a|b', s)) ------------------------------------------------------ ['a', 'b', 'a', 'a']
4銆佹ā鍧楁柟娉?/h2>
re.mathch(): 浠庡紑澶存悳绱?鎼滅储鍒颁簡灏辨湁,娌℃悳绱㈠埌灏辨槸none
s = 'abc123\ndef456' res = re.match('\d+', s) #浠庡紑澶存悳绱㈡暟瀛?鎼滅储鍒颁簡灏辨湁,娌℃悳绱㈠埌灏辨槸none print(res) ---------------------------------------------- None s = '123abc123\ndef456' res = re.match('\d+', s) print(res) #杩斿洖鐨勬槸涓€涓璞?print(res.group()) #瀵硅薄蹇呴』鐢╣roup()杩斿洖 ----------------------------------------------------- <re.Match object; span=(0, 3), match='123'>
re.search(): 鎼滅储绗竴涓尮閰嶇粨鏋?鎵惧埌浜嗗氨涓嶆壘浜?/p>
s = '123abc123\ndef456' res = re.search('\d+', s) print(res) print(res.group()) ------------------------------------------------------ 123
re.split(): 鎸夌収鍖归厤瑙勫垯鍒囧壊
s1 = 'abc324asdfk234lkjsf324lkj' print(re.split('\d+', s1)) ----------------------------------------------- ['abc', 'asdfk', 'lkjsf', 'lkj']
re.sub(): 鎸夌収鍖归厤瑙勫垯鏇挎崲(閲嶇偣)
s1 = 'abc324asdfk234lkjsf324lkj' print(re.sub('\d+', '***', s1)) ----------------------------------------------- abc***asdfk***lkjsf***lkj
re.subn(): 鎸夌収鍖归厤瑙勫垯鏇挎崲,骞惰鏁?/p>
s1 = 'abc324asdfk234lkjsf324lkj' print(re.subn('\d+', '***', s1)) ----------------------------------------------- ('abc***asdfk***lkjsf***lkj', 3)
5銆佸簲鐢ㄤ妇渚?/h2>
渚嬮锛?/p>
? 1.瀵逛簬瀛楃涓?code>Life234234is beautiful234because234of persistence
? 2.璇蜂娇鐢╮e妯″潡 涓€琛屼唬鐮?/strong> 杩樺師杩欏彞璇濅负
Life is beautiful because of persistence
import re s = 'Life234234is beautiful234because234of persistence' # 缁撴灉涓猴細Life is beautiful because of persistence
瑙g瓟锛?/p>
print(" ".join(re.sub('[0-9]', " ", s).split()))
浜屻€乼yping 妯″潡
1銆佸鍏ユ柟寮?/h2>
from typing import xxx
2銆佷綔鐢?/h2>
鎻愪緵鐢熸垚鍣ㄧ被鍨嬶紙cenerator锛夛紝鍙凯浠g被鍨嬶紙iterable锛夛紝杩唬鍣ㄧ被鍨嬶紙iterator锛変笁绉嶆暟鎹被鍨嬶紝闄愬埗鍑芥暟
3銆佹柟娉?/h2>
from typing import Generator,Iterable,Iterator # 鍙傛暟鐨勬暟鎹被鍨? 杩斿洖鍊?def func(i: int, f: float, b: bool, lt: list, tup: tuple, dic: dict,g:Generator) -> tuple: lis = [i, f, b, lt, tup, dic] return tuple(lis) # i, f, b, lt, tup, dic = func(1,2,3,4,5,6) # 涓嶉敊璇?鍙槸涓嶈鑼?def ger(): yield res = func(1, 2, True, [1, 2], (1, 2), 'a': 1,ger()) print(res) ----------------------------------------------------- (1, 2, True, [1, 2], (1, 2), 'a': 1)
涓夈€乧ollections 妯″潡
1銆佸鍏ユ柟寮?/h2>
from collections import xxx
2銆佷綔鐢?/h2>
鐢ㄤ簬澶嶆潅鐨勬暟鎹被鍨?/p>
3銆佹柟娉?/h2>
3.1 鏈夊悕鍏冪粍 namedtuple
from collections import namedtuple point = namedtuple('point',['x','y']) p = point(1,2) print(p.x) print(p.y) --------------------------------------------------- 1 2
3.2 榛樿瀛楀吀 defaultdict
from collections import defaultdict # dic = 'a':1 # print(dic['b']) dic = defaultdict(lambda :'nan') # dic = # 濡傛灉鎵句笉鍒拌祴浜嗕竴涓粯璁ゅ€?dic['a'] = 1 print(dic['a']) print(dic['c']) #鎵句笉鍒板叧閿瓧c缁檆璧嬩簡涓€涓粯璁ゅ€糿an ---------------------------------------------------------- 1 nan
3.3 鍙岀闃熷垪
deque
浣跨敤list瀛樺偍鏁版嵁鏃讹紝鎸夌储寮曡闂厓绱犲緢蹇紝浣嗘槸鎻掑叆鍜屽垹闄ゅ厓绱犲氨寰堟參浜嗭紝鍥犱负list鏄嚎鎬у瓨鍌紝鏁版嵁閲忓ぇ鐨勬椂鍊欙紝鎻掑叆鍜屽垹闄ゆ晥鐜囧緢浣庛€?/p>
deque鏄负浜嗛珮鏁堝疄鐜版彃鍏ュ拰鍒犻櫎鎿嶄綔鐨勫弻鍚戝垪琛紝閫傚悎鐢ㄤ簬闃熷垪鍜屾爤锛?/p>
# lis = [1,2,3] # 绾挎€ц〃 # lis.append(4) #鍦ㄥ悗闈㈣拷鍔? # print(lis) from collections import deque # 閾捐〃 de = deque([1,2,3]) de.append(4) #鍦ㄦ渶鍚庨潰杩藉姞4 print(de) de.appendleft(0) #鍦ㄦ渶鍓嶉潰杩藉姞0 print(de) # 榛樿鍒犻櫎宸﹁竟鐨?de.popleft() print(de) ------------------------------------------------------------- deque([1, 2, 3, 4])
'''姣旇緝楹荤儲鐨勬柟娉?#39;'' s= 'programming' # dic = # for i in s: # if i in dic: # dic[i]+=1 # else: # dic[i] =1 # print(dic) ------------------------------------------------------------- 'p': 1, 'r': 2, 'o': 1, 'g': 2, 'a': 1, 'm': 2, 'i': 1, 'n': 1
# 鏂版柟娉?from collections import Counter s= 'programming' c = Counter() # 瀛楀吀 for i in s: c[i] +=1 #鍦ㄥ唴閮ㄨ嚜宸辫繘琛屼簡涓€涓垽鏂?print(c) ----------------------------------------------------- Counter('r': 2, 'g': 2, 'm': 2, 'p': 1, 'o': 1, 'a': 1, 'i': 1, 'n': 1)
以上是关于re 妯″潡銆乼yping 妯″潡銆乧ollections 妯″潡的主要内容,如果未能解决你的问题,请参考以下文章