【问题标题】:What does the last line stand for in this Python function? [duplicate]这个 Python 函数的最后一行代表什么? [复制]
【发布时间】:2014-01-09 10:51:42
【问题描述】:
def f(x):
    xs=str(x)
    if len(xs) == 1:
        return int(xs)
    n = int(xs[0]) + int (xs[1])
    if len (xs) == 2:
        return n
    else:
        return n+f(xs[2:])

具体来说,f(xs[2:]) 是做什么的?

【问题讨论】:

  • 你在问什么?正如大家所说,xs[2:] 是切片符号。如果你问return n+f(......),那就是递归。

标签: python function slice


【解决方案1】:

它被称为slicing notation,它正在创建列表的副本,不包括前两项。

【讨论】:

  • 还要注意它会导致递归。
  • @gregb212 也许这是故意的 :)
  • 是的...这是一个测验,我没听懂...谢谢!
  • @AndraV 不客气 :) 如果对您有帮助,请考虑接受此答案meta.stackexchange.com/a/5235/235416
  • @thefourtheye 哦,我真傻……只是假设你看了一眼。绝对不是那个。
猜你喜欢
  • 2014-07-31
  • 1970-01-01
  • 2021-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多