【发布时间】:2023-03-07 18:19:01
【问题描述】:
只是一个警告:我最近才开始编程,Python 是我的第一语言,也是迄今为止唯一的语言。
是否有与.index() 以相反方式工作的内置函数?我正在寻找这个,因为我创建了一个 bool 函数,其中有一个整数列表,如果给定的整数列表是 [x^0, x^1, x^2, x^3, ...] 和 `否则为假。
我想在代码中说的是:
n >= 1
while the position(n+1) = position(1)*position(n)
for the length of the list
return True
otherwise
False.
是否有一个内置函数可以用来输入位置并返回列表中的项目?
list = [1,2,4,8,16]
position(4)
返回整数 16。
编辑:抱歉,我不知道如何格式化 好的,我的意思是:
def powers(base):
''' (list of str) -> bool
Return True if the given list of ints is a list of powers of
some int x of the form [x^0, x^1, x^2, x^3, ...] and False
otherwise.
>>> powers([1, 2, 4, 8])
True
>>> powers([1, 5, 25, 75])
False
'''
最终编辑:
我刚刚从这里 (https://docs.python.org/2/tutorial/datastructures.html) 浏览了所有可用的列表方法并阅读了描述。我要的东西不能作为列表方法使用:(
抱歉给您带来不便。
【问题讨论】:
-
list[4]怎么样?我建议通读基础知识,例如参见sopython.com/wiki/What_tutorial_should_I_read%3F -
我已经试过了,但我不能让它通用到适用于所有项目。
-
我会查看链接,谢谢。
-
什么意思“足够通用”?给minimal reproducible example。
-
@Marco 你的评论毫无意义......