Python: Is There a builtin that works similar but opposite to .index()?

2024/11/17 20:24:24

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 listreturn 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.

Answer

As an answer to:

Is there a builtin I could use to input the position and return the item in the list?

You just need to access the list with it's index as:

>>> my_list = [1,2,4,8,16]
>>> my_list[4]
16  # returns element at 4th index

And, this property is independent of language. All the languages supports this.


Based on your edit in the question, you may write your function as:

def check_value(my_list):# if len is less than 2if len(my_list) < 2:if my_list and my_list[0] == 1:return Trueelse: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 Falseelse:return True

Sample run:

>>> check_value([1, 2, 4, 8])
True
>>> check_value([1, 2, 4, 9])
False
>>> check_value([1, 5, 25, 75])
False
https://en.xdnf.cn/q/120158.html

Related Q&A

Get a subset of a data frame into a matrix

I have this data frame:I want just the numbers under August - September to be placed into a matrix, how can I do this?I tried this cf = df.iloc[:,1:12] which gives me but it gives me the headers as w…

Get Pairs of List Python [duplicate]

This question already has answers here:How can I iterate over overlapping (current, next) pairs of values from a list?(13 answers)Closed 9 years ago.c = [1,2,3,4,5,6,7,8,9,10]for a,b in func(c):doSome…

Python - make a list

How can I turn something like this: (not a file)Edit: Its what I get when I print this:adjusted_coordinate_list = str(adjusted_X_coordinate) + , + str(Y_coordinate)1,1 1,2 1,3 1,4 1,5into this:[1,1,1,2…

Error while running test case

I need to test my code below. I am using one test to see if it is working or not. but dont know what exactly I should pass as a parameter in the test code. Please see the test code at the end and pleas…

How to run something on each line of txt file?

I am trying to get this to run on each line of a .txt file.D1 =int(lines[0])*10 D2 =int(lines[1])*9 D3 =int(lines[2])*8 D4 =int(lines[3])*7 D5 =int(lines[4])*6 D6 =int(lines[5])*5 D7 =int(lines[6])*4 D…

Python np.nditer() - ValueError: Too many operands

I have a few methods which pass different amount of messy data to this function to combine headers with data and return a list of dictionaries:def zip_data(self, indicator_names, indicator_values):valu…

Python 3 - Find the Mode of a List

def mode(L):shows = []modeList = []L.sort()length = len(L)for num in L:count = L.count(num)shows.append(count)print List = , LmaxI = shows.index(max(shows))for i in shows:if i == maxI:if modeList == []…

Create list of sublists [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…

Python error - list index out of range?

Anyone help me see why I keep getting "list index out of range" as an error ?? def printInfo(average):average.sort() # sorts the list of tuples average.reverse() # reverses the list of tu…

Access strings inside a list

A Python list has [12:30,12:45] and I want to access the 12:30 for the first iteration, and on the second iteration I should get 12:45.my_list=[12:30,12:45] for each_value in my_list:print(each_value[0…