7

This is somewhat related to a question I asked not too long ago today. I am taking the intersection of two lists as follows:

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

The two components that I am taking the intersection of belong to two lengthy lists. Is it possible to get the indices of the intersected values? (The indices of the original lists that is).

I'm not quite sure where to start with this one.

Any help is greatly appreciated!

user1620716
  • 1,463
  • 6
  • 17
  • 26

1 Answers1

15

I would create a dictionary to hold the original indices:

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

Now, build your sets as before:

inter = set(ind_dict).intersection(catdate)

Now, to get a list of indices:

indices = [ ind_dict[x] for x in inter ]
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • in the second line, shouldn't "values()" be "keys()"? Since the dates are being stored as keys in the first line? – Kyle Simek Aug 11 '14 at 05:35
  • @KyleSimek -- Yes, I believe you're right. Ultimately, you could even do `set(NNSRCfile['datetimenew']).intersection(catdate)` _exactly_ as before. I'm not sure what I was thinking :-) – mgilson Aug 11 '14 at 06:10