只是一個預警:我剛剛開始編程,Python是我的第一語言,也是迄今爲止唯一的語言。Python:是否有一個內建工作與.index()相似但相反?
有沒有一種內部工作方式與.index()
相反?我在尋找這個,因爲我做了一個bool函數,我有一個int列表,我想返回True
,如果給定的int列表是[x^0, x^1, x^2, x^3, ...]
形式的某個int x的冪的列表,否則返回'False'。
我想在代碼中說的線沿線的是:
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
編輯:對不起,我不知道如何格式化在這裏 OK生病說明我的意思:
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]''怎麼樣?我建議閱讀基礎知識,例如參見http://sopython.com/wiki/What_tutorial_should_I_read%3F – jonrsharpe
我已經嘗試過了,但是我無法使其通用於申請所有項目。 – Marco
雖然我會檢查鏈接,謝謝。 – Marco