Python 越界列表索引的默认值

Python 越界列表索引的默认值,python,python-2.7,Python,Python 2.7,是否有一种标准方法可以从列表中获取一个项目,但返回一个默认值是不允许的 作为一个例子,我现在有一个这样的函数(好吧,它的许多变体,我的最新版本是用于读取CSV文件): 使用try except块并捕获索引器 >>> def testFunc(lst, index): try: return lst[index] except IndexError: return "abc" >>&g

是否有一种标准方法可以从列表中获取一个项目,但返回一个默认值是不允许的

作为一个例子,我现在有一个这样的函数(好吧,它的许多变体,我的最新版本是用于读取CSV文件):


使用
try except
块并捕获
索引器

>>> def testFunc(lst, index):
        try:
            return lst[index]
        except IndexError:
            return "abc"


>>> testFunc([1, 2, 3], 2)
3
>>> testFunc([1, 2, 3], 9)
'abc'
类似的问题讨论了为什么列表没有像字典那样的
get
方法

如果您确实想使用
If
语句,您可以只使用一行代码

>>> def testFunc(lst, index):
        return lst[index] if index < len(lst) else "abc"

>>> testFunc([1, 2, 3], 2)
3
>>> testFunc([1, 2, 3], 9)
'abc'
def testFunc(lst,索引): 如果索引>>testFunc([1,2,3],2) 3. >>>testFunc([1,2,3],9) “abc”
使用
尝试除外
块并捕获
索引器

>>> def testFunc(lst, index):
        try:
            return lst[index]
        except IndexError:
            return "abc"


>>> testFunc([1, 2, 3], 2)
3
>>> testFunc([1, 2, 3], 9)
'abc'
类似的问题讨论了为什么列表没有像字典那样的
get
方法

如果您确实想使用
If
语句,您可以只使用一行代码

>>> def testFunc(lst, index):
        return lst[index] if index < len(lst) else "abc"

>>> testFunc([1, 2, 3], 2)
3
>>> testFunc([1, 2, 3], 9)
'abc'
def testFunc(lst,索引): 如果索引>>testFunc([1,2,3],2) 3. >>>testFunc([1,2,3],9) “abc”
如果您不想覆盖列表的getimtem,您可以编写类似dict的get方法:

class fancyList_if(list):
    def get(self, index, default = None):
        if (index > len(self)):
            return default
        return self.__getitem__(index)
如果您很少期望越界,那么您可以将其作为EXE实现

class fancyList_except(list):
    def get(self, index, default = None):
        try:
            self.__getitem__(index)
        except IndexError:
            return default
benshmarks:

In [58]: a = fancyList_if((1,3,4,5))

In [59]: b = fancyList_except((1,3,4,5))

In [60]: %timeit a.get(2, 10)
1000000 loops, best of 3: 494 ns per loop

In [61]: %timeit a.get(10, 10)
1000000 loops, best of 3: 305 ns per loop

In [62]: %timeit b.get(2, 10)
1000000 loops, best of 3: 409 ns per loop

In [63]: %timeit b.get(10, 10)
1000000 loops, best of 3: 1.67 us per loop
盈亏平衡:

500命中+300 miss=400命中+1700未命中

命中/未命中=14


因此,如果您期望超过1/14的查找“失败”,那么请使用if语句,否则请使用try/catch。

如果您不想覆盖列表的getimtem,可以编写类似dict的get方法:

class fancyList_if(list):
    def get(self, index, default = None):
        if (index > len(self)):
            return default
        return self.__getitem__(index)
如果您很少期望越界,那么您可以将其作为EXE实现

class fancyList_except(list):
    def get(self, index, default = None):
        try:
            self.__getitem__(index)
        except IndexError:
            return default
benshmarks:

In [58]: a = fancyList_if((1,3,4,5))

In [59]: b = fancyList_except((1,3,4,5))

In [60]: %timeit a.get(2, 10)
1000000 loops, best of 3: 494 ns per loop

In [61]: %timeit a.get(10, 10)
1000000 loops, best of 3: 305 ns per loop

In [62]: %timeit b.get(2, 10)
1000000 loops, best of 3: 409 ns per loop

In [63]: %timeit b.get(10, 10)
1000000 loops, best of 3: 1.67 us per loop
盈亏平衡:

500命中+300 miss=400命中+1700未命中

命中/未命中=14


因此,如果您期望超过1/14的查找“失败”,则使用if语句,否则使用try/catch。

这将比仅检查长度更长。是的。但是,这是做你想做的事情的标准方法。如果你的身体不太可能出界,它也会更快。这比仅仅检查长度要长。是的。但是,这是做你想做的事情的标准方法。如果你不太可能越界,它也会更快。我真的不想改变现有函数的行为,因为那样会引起混乱。我真的不想改变现有函数的行为,因为那样会引起混乱。