a = [7, 5, 7, 7, 1, 2, 3, 2]
b = []
for i in a:
if i not in b:
b.append(i)
a = [7, 5, 7, 7, 1, 2, 3, 2]
b = []
s = set()
for i in a:
if i not in s:
b.append(i)
s.add(i)
import re
p = re.compile(r"\bм.*?\b", re.I) # \b — это граница слова, есичё
re.sub(p, "" , s)
" ".join([x for x in s.split() if not x.startswith(("м", "М"))])
" ".join(x for x in s.split() if not x.startswith(("м", "М")))
a = [4,6,7,8,1,5,3]
b = len(a) // 2
print(a[b:] + a[:b])
def f(a):
b = len(a) // 2
c = []
for i in range(len(a)):
idx = (i + b) % len(a)
c.append(a[idx])
return c
from operator import itemgetter
list2 = sorted(list1, key=itemgetter(0))
list2 = sorted(list1)
import difflib
from functools import lru_cache
from itertools import combinations
arr = [
{"_id": 1, "list_word_int": (189, 114, 188, 90, 2, 68, 96, 0, 250, 168, 150, 126)},
{"_id": 2, "list_word_int": (224, 26, 56, 153, 139, 128, 126, 220, 190, 137)},
{"_id": 3, "list_word_int": (188, 241, 225, 134, 134, 30, 134, 187, 204, 227, 3)},
{"_id": 4, "list_word_int": (224, 166, 159, 236, 82, 17, 82, 21, 227, 97)},
{"_id": 5, "list_word_int": (98, 96, 38, 107, 142, 134, 13, 36, 23)},
]
@lru_cache(maxsize=2 ** 13)
def get_ratio(lst1, lst2):
return difflib.SequenceMatcher(None, lst1, lst2).ratio()
if __name__ == "__main__":
for a, b in combinations(arr, 2):
ratio = get_ratio(a["list_word_int"], b["list_word_int"])
print(
"id= ",
a["_id"],
"Сравниваемый id=",
b["_id"],
"Коэффициент похожести:",
ratio,
)
print(get_ratio.cache_info())
import difflib
import multiprocessing as mp
import os
from itertools import combinations, cycle
arr = [
{"_id": 1, "list_word_int": [189, 114, 188, 90, 2, 68, 96, 0, 250, 168, 150, 126]},
{"_id": 2, "list_word_int": [224, 26, 56, 153, 139, 128, 126, 220, 190, 137]},
{"_id": 3, "list_word_int": [188, 241, 225, 134, 134, 30, 134, 187, 204, 227, 3]},
{"_id": 4, "list_word_int": [224, 166, 159, 236, 82, 17, 82, 21, 227, 97]},
{"_id": 5, "list_word_int": [98, 96, 38, 107, 142, 134, 13, 36, 23]},
]
def target(id_, count):
pid = os.getpid()
for i, (a, b) in zip(cycle(range(count)), combinations(arr, 2)):
if i != id_:
continue
ratio = difflib.SequenceMatcher(
None, a["list_word_int"], b["list_word_int"]
).ratio()
print(f"PID: {pid} id={a['_id']} & {b['_id']} ratio={ratio}")
if __name__ == "__main__":
processes = []
for x in range(mp.cpu_count()):
p = mp.Process(target=target, args=(x, mp.cpu_count()))
p.start()
processes.append(p)
for p in processes:
p.join()
import difflib
import multiprocessing as mp
import os
from itertools import combinations
arr = [
{"_id": 1, "list_word_int": [189, 114, 188, 90, 2, 68, 96, 0, 250, 168, 150, 126]},
{"_id": 2, "list_word_int": [224, 26, 56, 153, 139, 128, 126, 220, 190, 137]},
{"_id": 3, "list_word_int": [188, 241, 225, 134, 134, 30, 134, 187, 204, 227, 3]},
{"_id": 4, "list_word_int": [224, 166, 159, 236, 82, 17, 82, 21, 227, 97]},
{"_id": 5, "list_word_int": [98, 96, 38, 107, 142, 134, 13, 36, 23]},
]
def queue_creator(q, w_count):
pid = os.getpid()
print("Created queue generator PID", pid)
for a, b in combinations(arr, 2):
q.put((a, b))
for _ in range(w_count):
q.put(("stop", None))
def worker(q):
pid = os.getpid()
print("Created worker PID", pid)
while True:
a, b = q.get()
if a == "stop":
break
ratio = difflib.SequenceMatcher(
None, a["list_word_int"], b["list_word_int"]
).ratio()
print(f"PID:{pid} {a['_id']} & {b['_id']} ratio={ratio}")
if __name__ == "__main__":
queue = mp.Queue()
# 1 воркер на генерацию комбинаций, остальные на обработку
workers_count = (mp.cpu_count() - 1) or 1
q_process = mp.Process(target=queue_creator, args=(queue, workers_count))
q_process.start()
processes = [q_process]
for x in range(workers_count):
p = mp.Process(target=worker, args=(queue,))
p.start()
processes.append(p)
for process in processes:
process.join()
In [1]: x = 5
In [2]: 2 < x < 6
Out[2]: True
In [3]: x > 2 and x < 6
Out[3]: True
In [4]: %timeit 2 < x < 6
79.2 ns ± 0.301 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [5]: %timeit x > 2 and x < 6
80 ns ± 0.429 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
subprocess или os в связке с ping не подходит, так как вывод от ping идет в консоль программы