Python: finding matching indexes to intersect two lists

This is somewhat related to the question I asked not so long ago today. I take the intersection of the two lists as follows:

inter = set(NNSRCfile['datetimenew']).intersection(catdate) 

The two components that I take at the intersection belong to two long lists. Is it possible to get indices of intersecting values? (The indexes of the source lists, which are).

I'm not quite sure where to start from this.

Any help is much appreciated!

+4
source share
1 answer

I would create a dictionary for storing source indexes:

 ind_dict = dict((k,i) for i,k in enumerate(NNSRCfile['datetimenew'])) 

Now create your sets as before:

 inter = set(ind_dict).intersection(catdate) 

Now to get a list of indexes:

 indices = [ ind_dict[x] for x in inter ] 
+8
source

Source: https://habr.com/ru/post/1435981/


All Articles