Skip to main content
 首页 » 编程设计

Python:如果语句评估为真,则获取索引

2025年05月04日10Free-Thinker

我正在做一个简单的查找

if xo.st in sts: 
    #... 

如果满足条件,我需要获取 sts 中元素的索引(sts 是列表或元组)。这需要快速(这是一个大列表)。最好的解决方案是什么?

请您参考如下方法:

关于:

if xo.st in sts: 
    print sts.index(xo.st) 

这将返回 sts 中 xo.st 的第一个索引