Python: 是否有与 .index() 类似但相反的内置函数?
Python: Is There a builtin that works similar but opposite to .index()?
预先警告:我最近才开始编程,Python 是我的第一语言,也是迄今为止唯一的语言。
是否有与.index()
相反的内置函数?我正在寻找这个,因为我做了一个 bool 函数,其中我有一个 int 列表,如果给定的 int 列表是某个 int x 的幂列表,我想 return True
否则形成 [x^0, x^1, x^2, x^3, ...]
和 `False'。
我想在代码中说的是:
n >= 1
while the position(n+1) = position(1)*position(n)
for the length of the list
return True
otherwise
False.
是否有我可以用来输入列表中的位置和 return 项目的内置函数?
list = [1,2,4,8,16]
position(4)
returns 整数 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) 中所有可用的列表方法并阅读了说明。我所要求的,不能作为列表方法使用:(
对于给您带来的不便,我们深表歉意。
作为对以下问题的回答:
Is there a builtin I could use to input the position and return the item in the list?
您只需访问 list
,其索引为:
>>> my_list = [1,2,4,8,16]
>>> my_list[4]
16 # returns element at 4th index
而且,这个属性是独立于语言的。所有语言都支持这个。
根据您在问题中的编辑,您可以将函数编写为:
def check_value(my_list):
# if len is less than 2
if len(my_list) < 2:
if my_list and my_list[0] == 1:
return True
else:
return False
base_val = my_list[1] # as per the logic, it should be original number i.e num**1
for p, item in enumerate(my_list):
if item != base_val ** p:
return False
else:
return True
样本运行:
>>> check_value([1, 2, 4, 8])
True
>>> check_value([1, 2, 4, 9])
False
>>> check_value([1, 5, 25, 75])
False
def powers(n):
for i in itertools.count(0):
yield n**i
def is_powers(li):
if li[0] == 1:
if len(li) > 1:
return all(x==y for x,y in zip(li,powers(li[1])))
return True
return False
is_powers([1, 2, 4, 8])
is_powers([1, 5, 25, 75])
也许......它真的不清楚你在问什么......这假设它总是必须以 1 开头,如果它是有效的......
预先警告:我最近才开始编程,Python 是我的第一语言,也是迄今为止唯一的语言。
是否有与.index()
相反的内置函数?我正在寻找这个,因为我做了一个 bool 函数,其中我有一个 int 列表,如果给定的 int 列表是某个 int x 的幂列表,我想 return True
否则形成 [x^0, x^1, x^2, x^3, ...]
和 `False'。
我想在代码中说的是:
n >= 1
while the position(n+1) = position(1)*position(n)
for the length of the list
return True
otherwise
False.
是否有我可以用来输入列表中的位置和 return 项目的内置函数?
list = [1,2,4,8,16]
position(4)
returns 整数 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) 中所有可用的列表方法并阅读了说明。我所要求的,不能作为列表方法使用:(
对于给您带来的不便,我们深表歉意。
作为对以下问题的回答:
Is there a builtin I could use to input the position and return the item in the list?
您只需访问 list
,其索引为:
>>> my_list = [1,2,4,8,16]
>>> my_list[4]
16 # returns element at 4th index
而且,这个属性是独立于语言的。所有语言都支持这个。
根据您在问题中的编辑,您可以将函数编写为:
def check_value(my_list):
# if len is less than 2
if len(my_list) < 2:
if my_list and my_list[0] == 1:
return True
else:
return False
base_val = my_list[1] # as per the logic, it should be original number i.e num**1
for p, item in enumerate(my_list):
if item != base_val ** p:
return False
else:
return True
样本运行:
>>> check_value([1, 2, 4, 8])
True
>>> check_value([1, 2, 4, 9])
False
>>> check_value([1, 5, 25, 75])
False
def powers(n):
for i in itertools.count(0):
yield n**i
def is_powers(li):
if li[0] == 1:
if len(li) > 1:
return all(x==y for x,y in zip(li,powers(li[1])))
return True
return False
is_powers([1, 2, 4, 8])
is_powers([1, 5, 25, 75])
也许......它真的不清楚你在问什么......这假设它总是必须以 1 开头,如果它是有效的......