python - Python - 使用递归查找列表中给定元素的索引
问题描述
def ind(e, L):
if e not in L:
return 0
else:
return 1 + ind(e, L[:1])
assert ind(42, [55, 77, 42, 12, 42, 100])
我想要索引(所以在这种情况下我需要 2),但代码似乎总是给我数字。我也不能在 Python 中使用索引函数。
如果你有时间请帮忙。
解决方案
def ind(e, L):
if e == L[0]:
return 0
else:
return 1 + ind(e, L[1:])
推荐阅读
- javascript - 忽略部分字符串的正则表达式
- ruby - 如何观察实例变量并触发方法?
- jpa - DAO 类中的 get 方法(对于 OneToMany)是否总是进行数据库调用?
- sql-server - 带有 OUTPUT 子句的 INSERT INTO TEMP 表向不在源中的值添加额外字符
- ada - 在定点声明上使用 gnat 出现奇怪的编译错误
- php - 将项目从 xampp 文件夹更改为另一个文件夹并删除了以前的 xampp 版本
- oauth - 授予访问令牌后的 Oauth2 流程
- linux - 如何控制内核空间中的 LED 列表
- python - 散景(Python):在悬停工具提示中格式化日期时间
- kubernetes - 如何配置 Kubernetes 以接收 TCP 连接