optimize python algorithm function - find out what elements are added and deleted in second list
$80-240 HKD
Completato
Pubblicato più di 5 anni fa
$80-240 HKD
Pagato al completamento
basically, I need to compare two lists. I need to know what elements are added and deleted.
Here is my code but it's running slow. I would like to have "high speed" version function instead of this.
import itertools
def compareQueues2(prev:list, curr:list):
common_max_length = min(len(prev), len(curr))
m1=0
for pi in prev:
for ci in curr:
if pi == ci:
m1+=1
break
notfound = True
prev_same = []
curr_same = []
common_max_length=min(common_max_length, m1)
for n in range(common_max_length, 0, -1):
# print(n)
if notfound:
for p_it in [login to view URL](range(len(prev)), n):
# print (p_it)
if notfound:
for c_it in [login to view URL](range(len(curr)), n):
s = sum([prev[p_it[i]] == curr[c_it[i]] for i in range(n)])
if s == n:
#print("{} matches {}".format(p_it, c_it))
prev_same = p_it
curr_same = c_it
notfound = False
break
deleted = [i for i in range(len(prev)) if i not in prev_same]
added = [i for i in range(len(curr)) if i not in curr_same]
return prev_same, curr_same, deleted, added
---------------------
compareQueues2(['ab', 'will del', 'gh', 'cc'], ['ab', 'gh','added', 'cc'])
sample result: ((0, 2, 3), (0, 1, 3), [1], [2])
I'm an experienced Python developer and some of my skills are analyzing and optimizing algorithms. For that reason I think I'm the best candidate for this work.