If you are working as Python developer, data analysts or data scientists for any organisation then it is very important for you to know how to play with Lists and get the requested info such as matching indexes or items from them.
In Python, Lists store an ordered collection of items which can be of different types. Each item in a list has an assigned index value. It is important to note that Python is a zero indexed based language. All this means is that the first item in the list is at index 0.
If you want to get all the occurrences and the position of one or more items in a list by using Python then there are many ways but you need a very sufficient way to get the matching items from the list.
Let’s see the below example —
MyList = ['Mango', 'Orange', 'Banana', 'Apple', 'Grapes', 'Apple']
In the above list, you can see that Python based list is starting with 0 index and end with total item-1. In our cases, last index value in our fruit list will be 6–1 =5
Now, this point is How to get the occurrence of a item in the list. You could use a list comprehension with enumerate too such as given below -
# list of items
MyList = ['Mango', 'Orange', 'Banana', 'Apple', 'Grapes', 'Apple']#searchable item value
SeachItem='Apple'#store matching values through enumerate into a list comprehension
indexes = [n for n, x in enumerate(MyList) if x==SeachItem]#matching indexed value
indexes[3, 5]
# pandas library for data manipulation in python
import pandas as pd# list of items
MyList = ['Mango', 'Orange', 'Banana', 'Apple', 'Grapes', 'Apple']#convert list into series variable
series = pd.Series(MyList)#values in series variable
series# Display output0 Mango
1 Orange
2 Banana
3 Apple
4 Grapes
5 Apple
Now, we have converted list into series by using pandas library or package and we will be able to get a comparison check which will return a series of Booleans:
#searchable item value
SeachItem='Apple'#comparison check
series == SeachItem# Display output
0 False
1 False
2 False
3 True
4 False
5 True
If you try to pass that series of Booleans to the series via subscript notation, and you get just the matching members:
series[series == SeachItem]
# Display output
3 Apple
5 Apple
dtype: object
In this case, if you want them in a list or tuple, just pass them to the constructor as given below:
#list or tuple
list(series[series == SeachItem].index)# Display output
[3, 5]
Through For Loop — This is the another options to get the matching items from a given list, only for those coming from another language and may be with a simple loop it’s easier to understand and use it:
# list of items
MyList = ['Mango', 'Orange', 'Banana', 'Apple', 'Grapes', 'Apple']#searchable item valie
SeachItem='Apple'# converting a list comprehension
mlist = enumerate(MyList)#for loop
for index, item in mlist:
if item == SeachItem:
print(index, item)
# Display output
3 Apple
5 Apple
Interesting thoughts of Python index and matching of item list. I wonder, does e games online developer do these kind of indexing? Or they have some other other language aside from python
ReplyDelete