简体   繁体   English

Python:查找两个列表的交集的相应索引

[英]Python: Finding corresponding indices for an intersection of two lists

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! 任何帮助是极大的赞赏!

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 ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM