我有 2D 列表,我需要搜索元素的索引。由于我是编程初学者,所以我使用了以下函数:
def in_list(c):
for i in xrange(0,no_classes):
if c in classes[i]:
return i;
return -1
这里的类是一个二维列表,no_classes 表示类的数量,即列表的第一个维度。当 c 不在数组中时返回 -1。有什么我可以优化搜索吗?
原文由 ranger 发布,翻译遵循 CC BY-SA 4.0 许可协议
您不需要自己定义
no_classes
。使用enumerate()
: