未加星标

Python : How to find an element in Tuple by value

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二05 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

In this article we will discuss different ways to check if an element exists in tuple or not. If yes then also find its index and occurrence count.

Find an element in tuple by value using ‘in’ & ‘not in’

python provides operator in and not in to check if an element exists in tuple.

Suppose we have a tuple,

# A tuple of numbers tupleObj = (12 , 34, 45, 22, 33 , 67, 34, 56 )

Let’s check if element with value 34 exists in tuple using in operator,

# Check if element 34 exists in tuple if 34 in tupleObj: print("Element Found in Tuple") else: print("Element not Found in Tuple")

As 34 exists in tuple, so output will be,

Element Found in Tuple

Now let’s check if an element with value 1001 does not exists in tuple using not in operator,

# Check if element 1001 doesn't exists in tuple if 1001 not in tupleObj: print("Yes, element Not In tuple") else: print("Element is in Tuple")

As 1001 does not exists in tuple, so output will be,

Yes, element Not In tuple Find the index of an element in tuple using index()

Sometimes just checking if an element exists in tuple is not sufficient, we want to find it’s position of first occurrence in tuple.Tuple provides a member function index() i.e.

tuple.index(x)

It returns the index for first occurrence of x in the tuple. Also, if element is not found in tuple then it will throw an exception ValueError .

Check out these examples,

Example 1:

# Find the index of 34 in tuple, if not found then handle the exception try : pos = tupleObj.index(34) print("Element 34 Found at : " , pos) except ValueError as e: print(e)

As 34 exists in the tuple therefore output will be,

Element 34 Found at :1

Example 2:

# Find the index of 24 in tuple, if not found then handle the exception try : pos = tupleObj.index(24) print("Element 24 Found at : " , pos) except ValueError as e: print(e)

As 24 does not exists in the tuple therefore output will be,

tuple.index(x): x not in tuple Find the occurrence count of an element in the tuple using count()

Tuple provides an another member function count() i.e.

tuple.count(elem)

It returns the number of times elem appears in tuple.

Let’s find the occurrence count if element 34 in tuple i.e.

# Get the count of how many times 34 appears in tuple count = tupleObj.count(34) print("Count of 34 in tuple is : ", count)

As 34 exists in tuple multiple times, so output will be,

Count of 34 in tuple is :2

Based on occurrence count we can also check if element exists in tuple i.e.

if tupleObj.count(34) > 0 : print("34 Found in Tuple") else: print("34 Not Found in Tuple")

Output:

34 Found in Tuple

Complete example is as follows,

def main(): # A tuple of numbers tupleObj = (12 , 34, 45, 22, 33 , 67, 34, 56 ) print("**** Find an element in tuple using 'in' & 'not in' *****") # Check if element 34 exists in tuple if 34 in tupleObj: print("Element Found in Tuple") else: print("Element not Found in Tuple") # Check if element 1001 doesn't exists in tuple if 1001 not in tupleObj: print("Yes, element Not In tuple") else: print("Element is in Tuple") print("**** Find the index of an element in Tuple *****") # Find the index of 24 in tuple, if not found then handle the exception try : pos = tupleObj.index(24) print("Element 24 Found at : " , pos) except ValueError as e: print(e) # Find the index of 34 in tuple, if not found then handle the exception try : pos = tupleObj.index(34) print("Element 34 Found at : " , pos) except ValueError as e: print(e) print("**** Find the occurence count an element in the Tuple *****") # Get the count of how many times 34 appears in tuple count = tupleObj.count(34) print("Count of 34 in tuple is : ", count) # Based on occurrence count check if element exists in tuple if tupleObj.count(34) > 0 : print("34 Found in Tuple") else: print("34 Not Found in Tuple") if __name__ == '__main__': main() Output:

**** Find an element in tuple using 'in' & 'not in' ***** Element Found in Tuple Yes, element Not In tuple **** Find the index of an element in Tuple ***** tuple.index(x): x not in tuple Element 34 Found at :1 **** Find the occurence count an element in the Tuple ***** Count of 34 in tuple is :2 34 Found in Tuple Click Here to Subscribe for more Articles / Tutorials like this.

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

tags: tuple,element,print,Tuple,index,Found,count,exists
分页:12
转载请注明
本文标题:Python : How to find an element in Tuple by value
本站链接:https://www.codesec.net/view/586932.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(32)