How to know the index number of all specific numbers in a list in python
python find position in list
python list index
python find object in list
python index string
python find index of all occurrences in string
python find index of value in numpy array
Look at the following list of numbers:
dlist = [23.0, 28.6, 32.9, 34.9, 34.2, 31.0, 25.9, 20.1, 15.0, 11.8, 11.1, 13.1, 17.4, 23.0, 28.6, 32.9, 34.9, 34.2, 31.0, 25.9, 20.1, 15.0, 11.8, 11.1, 13.1, 17.4]
Now I try to find the index numbers of all the numbers in the list which are higher than 25. Then I want to make a list of those index numbers.
for i in dlist: if i >= 25: checklist.append(list_degrees.index(i))
Now the problem is that it registers some index numbers twice. For example: we see the first number above 25: 28.6. This number has the index number 1. If we look further, we see again 28.6, this should have the index number 14. Instead, it got the index number 1 again.
Here you see my "wrong" output:
[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]
Would someone have an answer to my problem? Thank you in advance!
You could do it like the example below:
dlist = [23.0, 28.6, 32.9, 34.9, 34.2, 31.0, 25.9, 20.1, 15.0, 11.8, 11.1, 13.1, 17.4, 23.0, 28.6, 32.9, 34.9, 34.2, 31.0, 25.9, 20.1, 15.0, 11.8, 11.1, 13.1, 17.4] checklist =  for idx, val in enumerate(dlist): if val >= 25: checklist.append(idx); print(checklist);
The reason your code wasn't working correctly is because
index returns the index of first occurance in that list, causing values later in the list to show up with a wrong index because they get the
index of the item with the same value that's also appearing earlier in the list.
5 Examples of How to Get Python list length (array, tuple, dictionary , How do you find the index of a number in a list in Python? The index() method searches an element in the list and returns its index. In simple terms, the index() method finds the given element in a list and returns its position. If the same element is present more than once, the method returns the index of the first occurrence of the element.
indices_greater_than_5 = [idx for idx,value in enumerate(my_values) if value > 5]
is one way you could do it with a simple list comprehension that uses
enumerate to count from
How do I find an item in a list Python? Index. The method index () returns the lowest index in the list where the element searched for appears. If any element which is not present is searched, it returns a ValueError. Lets try this: list_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] element = 3 list_numbers.index (element) 2.
index returns the index of the first occurrence of that item in your list (https://docs.python.org/3/tutorial/datastructures.html). This is why you only get the earlier appearing indexes, to gather all the indexes you would want to use
enumerate and only take the indexes of items with a value greater than
res = [i for i, v in enumerate(dlist) if v > 25] # [1, 2, 3, 4, 5, 6, 14, 15, 16, 17, 18, 19]
How do you find the length of a list in Python? Now let's say we want to be able to identify if certain numbers are in the list. For example, maybe we want to know if 26 is in the list. Here's how we would do it, using the in keyword (make sure these lines of code appear in your Python files after you've already declared the list): if 26 in coolNumbers: (print: "26 is a cool number") That's
As an alternative, you could use numpy's where function:
import numpy as np np.where(np.asarray(dlist) > 25) # array([ 1, 2, 3, 4, 5, 6, 14, 15, 16, 17, 18, 19])
Find all indexes of an item in list using list. As list. index() returns the index of first occurrence of an item in list. So, to find other occurrences of item in list, we will call list. When we refer to a particular index number of a string, Python returns the character that is in that position. Since the letter y is at index number 4 of the string ss = "Sammy Shark!", when we print ss we receive y as the output. Index numbers allow us to access specific characters within a string.
There are many method to achieve that, using index() etc. But sometimes require to find all the indices of a particular value in case it has multiple the list and check for that value and just append the value index in new list and print that. Look at the following list of numbers: dlist = [23.0, 28.6, 32.9, 34.9, 34.2, 31.0, 25.9, 20.1, 15.0, 11.8, 11.1, 13.1, 17.4, 23.0, 28.6, 32.9, 34.9, 34.2, 31.0, 25.9
In this tutorial, you will learn exclusively about the index() function. The index() method searches an element in the list and returns its position/index. It means lists can hold mixed values within itself, such as integers, floats, All of the above are examples of lists. list_x is a list of heterogeneous items The index () method returns the index of a substring inside the string (if found). If the substring is not found, it raises an exception. The syntax of index () method for string is: str.index (sub [, start [, end]] ) index () Parameters. The index () method takes three parameters: sub - substring to be searched in the string str.
Return Value from List index(). The index() method returns the index of the given element in the list. If the element is not found, a ValueError exception is raised. In this article we will discuss how to find index of a value in a Numpy array (both 1D & 2D) using numpy.where (). Let’s create a Numpy array from a list of numbers i.e. Now let’s see how to to search elements in this Numpy array. In the above numpy array element with value 15 occurs at different places let’s find all it’s indices i.e.
L.index(elem)returns the index of the first occurrence of