I was wondering if you could help me to speed up my python script.
I have two lists:
a=['a','b','c','d','e','f','g','h','i','j']
b=['b','f','g','j']
I want to create a list that will contain elements of b, but will have a length of a, with elements not in b replaced by something else, let’s say '-999'. Also, instead of having the actual elements (a,b,c…) I want to substitute that with the element’s index from b. So it would look like that:
c=['-999',0,'-999','-999','-999', 1, 2,'-999','-999',3]
My code for now is:
c=[]
counter=0
for each in a:
if each in b:
c.append(counter)
counter+=1
else:
c.append('-999')
It works fine, however, in real life my list a is 600 000 elements long, and there are actually 7 b lists that I need to iterate them over, all between 3k and 250k elements as well.
Any ideas on how to speed this up?
If the elements in
bare unique then you can try this:For repeated items you can use
defaultdict(list):