Just a forewarning: I just recently started programming and Python is my first language and only language so far.
Is there a builtin that works in the opposite way of .index()
? I'm looking for this because I made a bool function where I have a list of ints and I want to 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.
What I want to say in code is along the lines of:
n >= 1
while the position(n+1) = position(1)*position(n)
for the length of the list
return True
otherwise
False.
Is there a builtin I could use to input the position and return the item in the list?
list = [1,2,4,8,16]
position(4)
returns the int 16.
EDIT: sorry I don't know how to format on here
ok ill show what I mean:
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
'''
FINAL EDIT:
I just went through all of the available list methods from here (https://docs.python.org/2/tutorial/datastructures.html) and read the descriptions. What I'm asking for, isn't available as a list method :(
sorry for any inconvenience.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…