آموزش diff دو متن با difflib #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
import difflib
a = 'hello\nworld\n'
b = 'hello\nWORLD\n'
print(''.join(difflib.unified_diff(a.splitlines(True), b.splitlines(True))))
pythonhost.ir | پایتون هاست
@pythonhost_ir
آموزش Top-N با heapq #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
import heapq
xs = [5, 1, 9, 2, 7]
print(heapq.nlargest(3, xs))
pythonhost.ir | پایتون هاست
@pythonhost_ir
آموزش binary search با bisect #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
import bisect
xs = [1, 3, 5, 7]
print(bisect.bisect_left(xs, 6))
pythonhost.ir | پایتون هاست
@pythonhost_ir
آموزش Counter: پرتکرارترینها #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
from collections import Counter
print(Counter('aabccc').most_common(2))
pythonhost.ir | پایتون هاست
@pythonhost_ir
آموزش deque برای sliding window #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
from collections import deque
d = deque(maxlen=3)
for x in [1, 2, 3, 4]:
d.append(x)
print(list(d))
pythonhost.ir | پایتون هاست
@pythonhost_ir
آموزش itertools: combinations #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
import itertools
print(list(itertools.combinations([1, 2, 3, 4], 2)))
pythonhost.ir | پایتون هاست
@pythonhost_ir
آموزش itertools: product #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
import itertools
print(list(itertools.product([1, 2], ['a', 'b'])))
pythonhost.ir | پایتون هاست
@pythonhost_ir
آموزش itertools: groupby پشتسرهم #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
import itertools
s = 'aaabcc'
print([(k, len(list(g))) for k, g in itertools.groupby(s)])
pythonhost.ir | پایتون هاست
@pythonhost_ir
آموزش argparse مینیمال #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
import argparse
p = argparse.ArgumentParser()
p.add_argument('--n', type=int, default=3)
args = p.parse_args([])
print(args.n)
pythonhost.ir | پایتون هاست
@pythonhost_ir
آموزش logging با فرمت #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s')
logging.info('ready')
pythonhost.ir | پایتون هاست
@pythonhost_ir
آموزش traceback تمیز #پایتون
pythonhost.ir | پایتون هاست
@pythonhost_ir
import traceback
try:
1 / 0
except ZeroDivisionError:
print(traceback.format_exc().splitlines()[-1])
pythonhost.ir | پایتون هاست
@pythonhost_ir