from typing import Optional, Match, Pattern, Tuple
import re
def matchs(target: str) -> Optional[str]:
pat: Pattern[str] = re.compile(r"port\s*(\d+)|Logon ID:\s*(\w+)")
res: Optional[Match[str]] = re.search(pat, target)
if res:
g: Tuple[Optional[str], Optional[str]] = res.groups()
return g[0] if g[0] else g[1]
return None
不知道 Python 好在哪儿。就是每每用到它时,感觉写代码很愉悦就是了。比如这样我就觉得很方便:
In [79]: m1 = {'a': 1, 'b': 2}
In [80]: m2 = {'c': 3, 'd': 4}
In [81]: {**m1, **m2}
Out[81]: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
lst = [['a', 'b', 'c'], ['@', '#', '$', '%'], ['1', '2']]
1.
import itertools
[list(i) for i in itertools.product(*lst)]
2.
def cross(lst):
def swim(l, t, res):
if len(l) == 0:
res += [t]
return
for i in l[0]:
swim(l[1:], t + [i], res)
res = []
swim(lst, [], res)
return res