【问题标题】:Python obtain the index number of a list after checking with another listPython在与另一个列表检查后获取一个列表的索引号
【发布时间】:2020-05-25 20:21:06
【问题描述】:

我有两个列表:

L1 = ['a','b','c','d','e']
L2 = ['h','a','j','b','k','c']

我想获取 L2 中的元素的索引号,这些元素也在 L1 中,---> [1,3,5]。

类似

[i for i in L1 if i in L2]

将返回元素本身而不是元​​素编号。有没有像上面这样简单的方法?

【问题讨论】:

  • s = set(L1); print( [i for i, v in enumerate(L2) if v in s] )

标签: python arrays python-3.x list numpy


【解决方案1】:

应该这样做:

[i for i in range(len(L2)) if L2[i] in L1]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-24
    • 2010-11-03
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    相关资源
    最近更新 更多