Python:为两个列表的交集找到对应的索引 [英] Python: Finding corresponding indices for an intersection of two lists

查看:651
本文介绍了Python:为两个列表的交集找到对应的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与我不久前今天问的一个问题有关.我将两个列表的交集如下:

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 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 ]

这篇关于Python:为两个列表的交集找到对应的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆