Find last occurrence of any item of one list in another list in python -
I have the following 2 listings in Python:
ll = [500,500,500,501,500,502,500] mm =
[4 9, 5, 5, 502] < / pre> In the list, I want to know the position of the last chance of any item. I can do this for one element:
lane (ll) - 1 - Ll [:: - 1] Index (502)> gt; & Gt; 5 Take here [:: - 1]. Indices (502) 502 provides last position and gives lane -1 total length.
How do I extend the entire list to work for mm? I know that I can write a function, but another dragon is the way
if you want Last Index Ll = -
-
ll [500,500,500,501,500,502,500] mm = [49 9, 501502] d = {v: k for calculate in k, v (ll) if v in mm} # {501: 3, 502: 5} Instead of o (n) it is possible to create a set to set before creating a (1) lookup, but three items For this, it is not really worth following the apro worries about maintaining unavailable index as any and make it a O (1) lookup Is using a hash lookup for. . # Create a key- gt; Do not have any word for all `mm`d = dict.fromkeys (mm) - com d. For update (k), (v, v) enumerate (ll) if in v = d) # {499: none, 501: 3, 502: 5}
Comments
Post a Comment