Python – Find frequency of given Datatype in tuple
Sometimes, while working with Python records, we can have a problem in which we need to extract count of any data type occurred in tuple. This can have application in various domains such as day-day programming and web development. Let’s discuss certain ways in which this task can be performed.
Input : test_tuple = (5, ‘Gfg’, 2, 8.8, 1.2, ‘is’), data_type = int
Output : 2
Input : test_tuple = (5, ‘Gfg’, 2, 8.8, 1.2, ‘is’), data_type = str
Output : 2
Method #1 : Using loop + isinstance() The combination of above functions can be used to solve this problem. In this, we perform the task of checking for data type using isinstance() and run a counter to increment on match.
Python3
test_tuple = ( 5 , 'Gfg' , 2 , 8.8 , 1.2 , 'is' )
print ("The original tuple : " + str (test_tuple))
data_type = float
count = 0
for ele in test_tuple:
if isinstance (ele, float ):
count = count + 1
print ("The data type frequency : " + str (count))
|
Output :
The original tuple : (5, 'Gfg', 2, 8.8, 1.2, 'is')
The data type frequency : 2
Method #2 : Using sum() + isinstance() The combination of above functions can also be used to solve this problem. This used similar way of solving as above method, just in shorthand way using sum() for counting.
Python3
test_tuple = ( 5 , 'Gfg' , 2 , 8.8 , 1.2 , 'is' )
print ("The original tuple : " + str (test_tuple))
data_type = float
count = sum ( 1 for ele in test_tuple if isinstance (ele, data_type))
print ("The data type frequency : " + str (count))
|
Output :
The original tuple : (5, 'Gfg', 2, 8.8, 1.2, 'is')
The data type frequency : 2
Method #3 : Using type() method. type() method returns the datatype of variable.
Python3
test_tuple = ( 5 , 'Gfg' , 2 , 8.8 , 1.2 , 'is' )
print ( "The original tuple : " + str (test_tuple))
data_type = float
count = 0
for ele in test_tuple:
if type (ele) is data_type:
count + = 1
print ( "The data type frequency : " + str (count))
|
Output
The original tuple : (5, 'Gfg', 2, 8.8, 1.2, 'is')
The data type frequency : 2
Method#4: Using filter()
Python3
test_tuple = ( 5 , 'Gfg' , 2 , 8.8 , 1.2 , 'is' )
print ( "The original tuple : " + str (test_tuple))
data_type = float
count = len ( list ( filter ( lambda ele: type (ele) is data_type, test_tuple)))
print ( "The data type frequency : " + str (count))
|
Output
The original tuple : (5, 'Gfg', 2, 8.8, 1.2, 'is')
The data type frequency : 2
Time complexity: O(n)
Space complexity: O(1)
Method#5: Using Recursive method.
Python3
def count_data_type(tup, data_type, count = 0 ):
if not tup:
return count
if type (tup[ 0 ]) is data_type:
count + = 1
return count_data_type(tup[ 1 :], data_type, count)
test_tuple = ( 5 , 'Gfg' , 2 , 8.8 , 1.2 , 'is' )
print ( "The original tuple : " + str (test_tuple))
data_type = float
count = count_data_type(test_tuple, data_type)
print ( "The data type frequency:" , count)
|
Output
The original tuple : (5, 'Gfg', 2, 8.8, 1.2, 'is')
The data type frequency: 2
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #6: Using a reduce() function and lambda function
Algorithm:
1.Import the reduce function from the functools module.
2.Initialize the input tuple and the data type whose frequency is to be counted.
3.Use the reduce() function to count the number of elements in the tuple that are of the specified data type. The reduce() function applies the 4.lambda function to each element of the tuple, which checks whether the element is of the specified data type. If it is, it increments the count by 1.
5.Print the count of the specified data type in the tuple.
Python3
from functools import reduce
test_tuple = ( 5 , 'Gfg' , 2 , 8.8 , 1.2 , 'is' )
print ( "The original tuple : " + str (test_tuple))
data_type = float
count = reduce ( lambda x, y: x + isinstance (y, data_type), test_tuple, 0 )
print ( "The data type frequency : " + str (count))
|
Output
The original tuple : (5, 'Gfg', 2, 8.8, 1.2, 'is')
The data type frequency : 2
Time Complexity: O(n), where n is the number of elements in the tuple. The reduce() function iterates over each element of the tuple once.
Auxiliary Space: O(1), since only constant extra space is used to store the count variable.
Method #7: Using list comprehension and the built-in function type():
Step-by-step approach:
- Initialize a tuple test_tuple.
- Print the original tuple.
- Initialize the data type data_type as float.
- Use a list comprehension to create a list of all elements in test_tuple that have the same data type as data_type.
- Find the length of the list generated in step 4 using the built-in function len().
- Print the result.
Python3
test_tuple = ( 5 , 'Gfg' , 2 , 8.8 , 1.2 , 'is' )
print ( "The original tuple : " + str (test_tuple))
data_type = float
count = len ([x for x in test_tuple if type (x) = = data_type])
print ( "The data type frequency : " + str (count))
|
Output
The original tuple : (5, 'Gfg', 2, 8.8, 1.2, 'is')
The data type frequency : 2
Time complexity: O(n), where n is the length of the tuple.
Auxiliary space: O(k), where k is the number of elements in the tuple that have the same data type as data_type.
Last Updated :
16 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...