【问题标题】:Why I cant pass keyword argument to list.index() method?为什么我不能将关键字参数传递给 list.index() 方法?
【发布时间】:2019-07-27 20:20:05
【问题描述】:

我正在查看 python 中 list.index() 方法的文档,我看到的是:

>>> help(list().index)
Help on built-in function index:

index(value, start=0, stop=9223372036854775807, /) method of builtins.list
instance
Return first index of value.

Raises ValueError if the value is not present.

当我运行下面的代码时出现了一些错误。

>>> l=[1,2,3,43,45,5,6,6]
>>> l.index(43,start=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: index() takes no keyword arguments

【问题讨论】:

    标签: python python-3.x list keyword-argument


    【解决方案1】:

    之前的文档在仅位置参数方面一直很差,但在现代 Python 上,它们正在改进。关键信息是签名中看似格格不入的/

    index(value, start=0, stop=9223372036854775807, /)
                                                    ^ This is not a typo!
    

    这意味着正斜杠之前的所有参数都只是位置参数,它们不能通过关键字传递。每the Programming FAQ for "What does the slash(/) in the parameter list of a function mean?"

    函数参数列表中的斜杠表示它之前的参数是仅位置参数。仅位置参数是没有外部可用名称的参数。在调用接受仅位置参数的函数时,参数仅根据其位置映射到参数。

    【讨论】:

      【解决方案2】:

      错误消息显示index 不接受关键字参数,但您提供的参数为 start=1

      代替:l.index(43, start=1) 使用:l.index(43, 1)

      至于解释,this可以解释:

      许多内置函数仅使用 METH_VARARGS,这意味着它们 不支持关键字参数。 "len" 更简单,使用 选项 METH_O 这意味着它获取单个对象作为参数。这个 保持代码非常简单,也可能对 性能。

      【讨论】:

      • 我使用它作为一个,但我根据文档询问社区我试图传递一个关键字参数并且出现错误是有什么问题还是有任何 PEP 指南相同?跨度>
      【解决方案3】:

      索引(值,开始=0,停止=9223372036854775807,/)

      文档在这里说,单词的含义只不过是您传递这些参数的目的。为了更好地理解这些参数的工作原理,请参见下面的代码示例 - list1 是 10 个元素的列表,索引函数采用 3 个参数作为开始和停止参数是可选的,为了切片列表,index function documentation explains it clear here -

      list1 = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
      for x in list1:
          print(list1.index(x, 0, 5))
      

      输出

      0 7                                                                                                                     
      1 1                                                                                                                     
      2 4                                                                                                                     
      3 9                                                                                                                     
      4 16                                                                                                                    
      Traceback (most recent call last):                                                                                      
        File "main.py", line 13, in <module>                                                                                  
          print(list1.index(x, 0, 5), x)                                                                                      
      ValueError: 25 is not in list
      

      代码从起点(即 0)和终点(即 5)开始寻找元素,但 for 循环 领先于第 5 个索引并引发值错误异常

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-03
        • 2022-01-23
        • 2010-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-24
        • 1970-01-01
        相关资源
        最近更新 更多