【问题标题】:Python: Finding corresponding indices for an intersection of two listsPython:为两个列表的交集查找相应的索引
【发布时间】:2012-09-16 05:32:15
【问题描述】:

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

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

我取交集的两个组件属于两个冗长的列表。是否可以获得相交值的索引? (即原始列表的索引)。

我不太确定从哪里开始。

非常感谢任何帮助!

【问题讨论】:

    标签: python indices set-intersection


    【解决方案1】:

    我会创建一个字典来保存原始索引:

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

    现在,像以前一样构建你的集合:

    inter = set(ind_dict).intersection(catdate)
    

    现在,获取索引列表:

    indices = [ ind_dict[x] for x in inter ]
    

    【讨论】:

    • 在第二行,“values()”不应该是“keys()”吗?由于日期被存储为第一行的键?
    • @KyleSimek -- 是的,我相信你是对的。最终,你甚至可以像以前一样 exactly 做到set(NNSRCfile['datetimenew']).intersection(catdate)。我不确定我在想什么:-)
    猜你喜欢
    • 1970-01-01
    • 2010-10-13
    • 2013-10-12
    • 2021-03-18
    • 1970-01-01
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多