import textwrap # 多行文本处理,格式化文本段落(缩进) sample_text = ''' The textwrap module can be used to format text for output in situations where pretty-printing is desired. It offers programmatic functionality similar to the paragraph wrapping or filling features found in many text editors. ''' dedented_text = textwrap.dedent(sample_text).strip() print(dedented_text) print() for width in [45, 60]: print('{} Columns:\n'.format(width)) print(textwrap.fill(dedented_text, width=width)) print()
The textwrap module can be used to format text for output in
situations where pretty-printing is desired. It offers
programmatic functionality similar to the paragraph wrapping
or filling features found in many text editors.
45 Columns:
The textwrap module can be used to format
text for output in situations where pretty-
printing is desired. It offers programmatic
functionality similar to the paragraph
wrapping or filling features found in many
text editors.
60 Columns:
The textwrap module can be used to format text for output in
situations where pretty-printing is desired. It offers
programmatic functionality similar to the paragraph wrapping
or filling features found in many text editors.
The textwrap module can be used to format text for
output in situations where pretty-printing is
desired. It offers programmatic functionality
similar to the paragraph wrapping or filling
features found in many text editors.
# 1.直接用re模块的函数搜索 # 2.先用compile编译,然后用编译过的搜索 # 匹配 pattern = 'this' text = 'Does this text match the pattern?'
match = re.search(pattern, text)
s = match.start() e = match.end()
print('Found "{}"\nin "{}"\nfrom {} to {} ("{}")'.format( match.re.pattern, match.string, s, e, text[s:e]))
# 查询所有 text = 'abbaaabbbbaaaaa'
pattern = 'ab'
formatchin re.findall(pattern, text): print('Found {!r}'.format(match)) text = 'This is some text -- with punctuation.'
print(text) print() # 组匹配 patterns = [ (r'^(\w+)', 'word at start of string'), (r'(\w+)\S*$', 'word at end, with optional punctuation'), (r'(\bt\w+)\W+(\w+)', 'word starting with t, another word'), (r'(\w+t)\b', 'word ending with t'), ]
for pattern, desc in patterns: regex = re.compile(pattern) match = regex.search(text) print("'{}' ({})\n".format(pattern, desc)) print(' ', match.groups()) print() # 替换 re.sub() # 拆分 re.split()
Found "this"
in "Does this text match the pattern?"
from 5 to 9 ("this")
Found 'ab'
Found 'ab'
This is some text -- with punctuation.
'^(\w+)' (word at start of string)
('This',)
'(\w+)\S*$' (word at end, with optional punctuation)
('punctuation',)
'(\bt\w+)\W+(\w+)' (word starting with t, another word)
('text', 'with')
'(\w+t)\b' (word ending with t)
('text',)
import difflib # 字符串比较序列 text1 = """Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer eu lacus accumsan arcu fermentum euismod. Donec pulvinar porttitor tellus. Aliquam venenatis. Donec facilisis pharetra tortor. In nec mauris eget magna consequat convalis. Nam sed sem vitae odio pellentesque interdum. Sed consequat viverra nisl. Suspendisse arcu metus, blandit quis, rhoncus ac, pharetra eget, velit. Mauris urna. Morbi nonummy molestie orci. Praesent nisi elit, fringilla ac, suscipit non, tristique vel, mauris. Curabitur vel lorem id nisl porta adipiscing. Suspendisse eu lectus. In nunc. Duis vulputate tristique enim. Donec quis lectus a justo imperdiet tempus."""
text1_lines = text1.splitlines()
text2 = """Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer eu lacus accumsan arcu fermentum euismod. Donec pulvinar, porttitor tellus. Aliquam venenatis. Donec facilisis pharetra tortor. In nec mauris eget magna consequat convalis. Nam cras vitae mi vitae odio pellentesque interdum. Sed consequat viverra nisl. Suspendisse arcu metus, blandit quis, rhoncus ac, pharetra eget, velit. Mauris urna. Morbi nonummy molestie orci. Praesent nisi elit, fringilla ac, suscipit non, tristique vel, mauris. Curabitur vel lorem id nisl porta adipiscing. Duis vulputate tristique enim. Donec quis lectus a justo imperdiet tempus. Suspendisse eu lectus. In nunc."""
Lorem ipsum dolor sit amet, consectetuer adipiscing
elit. Integer eu lacus accumsan arcu fermentum euismod. Donec
- pulvinar porttitor tellus. Aliquam venenatis. Donec facilisis
+ pulvinar, porttitor tellus. Aliquam venenatis. Donec facilisis
? +
- pharetra tortor. In nec mauris eget magna consequat
? -
+ pharetra tortor. In nec mauris eget magna consequat
- convalis. Nam sed sem vitae odio pellentesque interdum. Sed
? - --
+ convalis. Nam cras vitae mi vitae odio pellentesque interdum. Sed
? +++ +++++ +
consequat viverra nisl. Suspendisse arcu metus, blandit quis,
rhoncus ac, pharetra eget, velit. Mauris urna. Morbi nonummy
molestie orci. Praesent nisi elit, fringilla ac, suscipit non,
tristique vel, mauris. Curabitur vel lorem id nisl porta
- adipiscing. Suspendisse eu lectus. In nunc. Duis vulputate
- tristique enim. Donec quis lectus a justo imperdiet tempus.
+ adipiscing. Duis vulputate tristique enim. Donec quis lectus a
+ justo imperdiet tempus. Suspendisse eu lectus. In nunc.
# 每个东西都有一个name一个value for status in BugStatus: print('{:15} = {}'.format(status.name, status.value)) # 如果存在多个值,下面出现的就是别名,不想出现多个值,使用@unique print('\nSame: by_design is wont_fix: ', BugStatus.by_design is BugStatus.wont_fix) print('Same: closed is fix_released: ', BugStatus.closed is BugStatus.fix_released)
print('Before:') for k, v in d.items(): print(k, v)
d.move_to_end('b')
print('\nmove_to_end():') for k, v in d.items(): print(k, v)
d.move_to_end('b', last=False)
print('\nmove_to_end(last=False):') for k, v in d.items(): print(k, v)
Before:
a A
b B
c C
move_to_end():
a A
c C
b B
move_to_end(last=False):
b B
a A
c C
collections.abc
array数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import array
import binascii # 返回二进制数据 data 的十六进制表示形式。 data 的每个字节都被转换为相应的2位十六进制表示形式。
a = array.array('i', range(3)) print('Initial :', a)
s = b'This is the array.' a = array.array('b', s) print('As byte string:', s) print('As array :', a) print('As hex :', binascii.hexlify(a)) # 与序列类似的函数 print(chr(a[0]))
Initial : array('i', [0, 1, 2])
As byte string: b'This is the array.'
As array : array('b', [84, 104, 105, 115, 32, 105, 115, 32, 116, 104, 101, 32, 97, 114, 114, 97, 121, 46])
As hex : b'54686973206973207468652061727261792e'
T
# Write the array of numbers to a temporary file output = tempfile.NamedTemporaryFile() a.tofile(output.file) # must pass an *actual* file output.flush()
# Read the raw data withopen(output.name, 'rb') asinput: raw_data = input.read() print('Raw Contents:', binascii.hexlify(raw_data))
# Read the data into an array input.seek(0) a2 = array.array('i') a2.fromfile(input, len(a)) print('A2:', a2)
l = [] for i in values: # 默认右插 position = bisect.bisect(l, i) bisect.insort(l, i) # position = bisect.bisect_left(l, i) # bisect.insort_left(l, i) print('{:3} {:3}'.format(i, position), l)
def__deepcopy__(self, memo): print('\nCalling __deepcopy__ for {!r}'.format(self)) if self in memo: existing = memo.get(self) print(' Already copied to {!r}'.format(existing)) return existing print(' Memo dictionary:') if memo: for k, v in memo.items(): print(' {}: {}'.format(k, v)) else: print(' (empty)') dup = Graph(copy.deepcopy(self.name, memo), []) print(' Copying to new object {}'.format(dup)) memo[self] = dup for c in self.connections: dup.add_connection(copy.deepcopy(c, memo)) return dup
# memo字典跟踪已复制的对象避免递归 root = Graph('root', []) a = Graph('a', [root]) b = Graph('b', [a, root]) root.add_connection(a) root.add_connection(b)
logging.debug('Logging pformatted data') # 格式化转成字符串 formatted = pformat(data) for line in formatted.splitlines(): logging.debug(line.rstrip()) # print(line.rstrip())
from functools import partial # 将一个函数的部分参数预先绑定为某些值,从而得到一个新的具有较少可变参数的函数
# 普通函数 defadd(a,b,*args, **kwargs): print(f'a:{a}, b:{b}') # 打印位置参数 for n in args: print(n) print("-"*20) # 打印关键字参数 for k, v in kwargs.items(): print('%s:%s' % (k, v)) print("-"*20)
print('\nComparisons:') for expr in ['a < b', 'a <= b', 'a == b', 'a >= b', 'a > b']: print('\n{:<6}:'.format(expr)) result = eval(expr) print(' result of {}: {}'.format(expr, result))
Methods:
[('__eq__', <function MyObject.__eq__ at 0x0000021B998A7940>),
('__ge__', <function _ge_from_gt at 0x0000021B932024C0>),
('__gt__', <function MyObject.__gt__ at 0x0000021B998A70D0>),
('__init__', <function MyObject.__init__ at 0x0000021B998A79D0>),
('__le__', <function _le_from_gt at 0x0000021B93202550>),
('__lt__', <function _lt_from_gt at 0x0000021B93202430>)]
Comparisons:
a < b :
testing __gt__(1, 2)
testing __eq__(1, 2)
result of a < b: True
a <= b:
testing __gt__(1, 2)
result of a <= b: True
a == b:
testing __eq__(1, 2)
result of a == b: False
a >= b:
testing __gt__(1, 2)
testing __eq__(1, 2)
result of a >= b: False
a > b :
testing __gt__(1, 2)
result of a > b: False
print(sorted(['b29s', 'c2s20', 'a1-1', '88d'], key=cmp_to_key(mycmp))) print(sorted(['b29s', 'c2s20', 'a1-1', '88d'], key=lambda x: int(''.join([i for i in x if i.isdigit()]))))
220 vs 29 = 1
11 vs 220 = -1
11 vs 220 = -1
11 vs 29 = -1
88 vs 29 = 1
88 vs 220 = -1
['a1-1', 'b29s', '88d', 'c2s20']
['a1-1', 'b29s', '88d', 'c2s20']
print('First set of calls:') for i inrange(MAX): for j inrange(MAX): expensive(i, j) print(expensive.cache_info()) print('\nSecond set of calls:') for i inrange(MAX + 1): for j inrange(MAX + 1): expensive(i, j) print(expensive.cache_info()) print('\nClearing cache:') expensive.cache_clear() print(expensive.cache_info()) print('\nThird set of calls:') for i inrange(MAX): for j inrange(MAX): expensive(i, j) print(expensive.cache_info())
First set of calls:
expensive(0, 0)
expensive(0, 1)
expensive(1, 0)
expensive(1, 1)
CacheInfo(hits=0, misses=4, maxsize=128, currsize=4)
Second set of calls:
expensive(0, 2)
expensive(1, 2)
expensive(2, 0)
expensive(2, 1)
expensive(2, 2)
CacheInfo(hits=4, misses=9, maxsize=128, currsize=9)
Clearing cache:
CacheInfo(hits=0, misses=0, maxsize=128, currsize=0)
Third set of calls:
expensive(0, 0)
expensive(0, 1)
expensive(1, 0)
expensive(1, 1)
CacheInfo(hits=0, misses=4, maxsize=128, currsize=4)
import functools from functools import reduce # def reduce(function, iterable, initializer=None): # it = iter(iterable) # if initializer is None: # value = next(it) # else: # value = initializer # for element in it: # value = function(value, element) # return value
# count无限产生值 for i inzip(count(start=1, step=10), ['a', 'b', 'c']): print('{}: {}'.format(*i))
# cycle无限重复值 for i inzip(range(7), cycle(['a', 'b', 'c'])): print(i)
# repear重复n次值,不提供就是无限 for i in repeat([1,2,3,4], 5): print(i) # 组合 for i, s inzip(count(), repeat('over-and-over', 5)): print(i, s) for i inmap(lambda x, y: (x, y, x * y), repeat(2), range(5)): print('{:d} * {:d} = {:d}'.format(*i))
from itertools import dropwhile,takewhile,filterfalse,compress defshould_drop(x): print('Testing:', x) return x < 1 # 第一次为false之后的元素都会返回 for i in dropwhile(should_drop, [-1, 0, 1, 2, -2]): print('Yielding:', i) print('------------------------')
defshould_take(x): print('Testing:', x) return x < 2 # 第一次为false之前的元素都会返回,遇见false就不返回了 for i in takewhile(should_take, [-1, 0, 1, 2, -2]): print('Yielding:', i) print('------------------------')
# 只返回true的元素,每一个都会计算 defcheck_item(x): print('Testing:', x) return x < 1 for i infilter(check_item, [-1, 0, 1, 2, -2]): print('Yielding:', i) print('------------------------')
# 相反 defcheck_item(x): print('Testing:', x) return x < 1 for i in filterfalse(check_item, [-1, 0, 1, 2, -2]): print('Yielding:', i) print('------------------------')
d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3) d = [(1,2),(2,3),(3,4),(4,2)] di = sorted(d, key=lambda x:x[1]) for k, g in groupby(di, key=lambda x:x[1]): print(k, list(map(lambda x:x[1], g)))
for card in DECK: print('{:>2}{}'.format(*card), end=' ') if card[1] == SUITS[-1]: print() # repear控制几个自身 defshow(iterable): for i, item inenumerate(iterable, 1): print(item, end=' ') if (i % 3) == 0: print() print()
from itertools import permutations,combinations,combinations_with_replacement defshow(iterable): first = None for i, item inenumerate(iterable, 1): if first != item[0]: if first isnotNone: print() first = item[0] print(''.join(item), end=' ') print()
All permutations:
abcd abdc acbd acdb adbc adcb
bacd badc bcad bcda bdac bdca
cabd cadb cbad cbda cdab cdba
dabc dacb dbac dbca dcab dcba
Pairs:
ab ac ad
ba bc bd
ca cb cd
da db dc
Unique pairs:
ab ac ad
bc bd
cd
Unique pairs:
aa ab ac ad
bb bc bd
cc cd
dd
operator:内置操作符的函数结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
from operator import *
# 关键字的函数实现
a = -1 b = 5
print('a =', a) print('b =', b) print()
print('not_(a) :', not_(a)) print('truth(a) :', truth(a)) print('is_(a, b) :', is_(a, b)) print('is_not(a, b):', is_not(a, b)) for func in (lt, le, eq, ne, ge, gt): print('{}(a, b): {}'.format(func.__name__, func(a, b)))