Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Find frequency of given Datatype in tuple

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




# Python3 code to demonstrate working of
# Data type frequency in tuple
# Using loop + isinstance()
 
# initializing tuples
test_tuple = (5, 'Gfg', 2, 8.8, 1.2, 'is')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# initializing data type
data_type = float
 
# Data type frequency in tuple
# Using loop + isinstance()
count = 0
for ele in test_tuple:
    if isinstance(ele, float):
        count = count + 1
 
# printing result
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




# Python3 code to demonstrate working of
# Data type frequency in tuple
# Using sum() + isinstance()
 
# initializing tuples
test_tuple = (5, 'Gfg', 2, 8.8, 1.2, 'is')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# initializing data type
data_type = float
 
# Data type frequency in tuple
# Using sum() + isinstance()
count = sum(1 for ele in test_tuple if isinstance(ele, data_type))
 
# printing result
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




# Python3 code to demonstrate working of
# Data type frequency in tuple
 
 
# initializing tuples
test_tuple = (5, 'Gfg', 2, 8.8, 1.2, 'is')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# initializing data type
data_type = float
 
# Data type frequency in tuple
count = 0
for ele in test_tuple:
    if type(ele) is data_type:
        count+=1
 
# printing result
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




# Python3 code to demonstrate working of
# Data type frequency in tuple
 
# initializing tuples
test_tuple = (5, 'Gfg', 2, 8.8, 1.2, 'is')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# initializing data type
data_type = float
 
# Data type frequency in tuple using filter
count = len(list(filter(lambda ele: type(ele) is data_type, test_tuple)))
 
# printing result
print("The data type frequency : " + str(count))
#This code is contributed by Vinay Pinjala.


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




# Python3 code to demonstrate working of
# Data type frequency in tuple
#using recursive method
def count_data_type(tup, data_type, count=0):
    if not tup:  #base condition
        return count
    if type(tup[0]) is data_type:
        count += 1
    return count_data_type(tup[1:], data_type, count)
 
  # initializing tuples
test_tuple = (5, 'Gfg', 2, 8.8, 1.2, 'is')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# initializing data type
data_type = float
 
count = count_data_type(test_tuple, data_type)
# printing result
print("The data type frequency:", count)
#this code contributed by tvsk.


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
 
# initializing tuples
test_tuple = (5, 'Gfg', 2, 8.8, 1.2, 'is')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# initializing data type
data_type = float
 
# Data type frequency in tuple using reduce
count = reduce(lambda x, y: x + isinstance(y, data_type), test_tuple, 0)
 
# printing result
print("The data type frequency : " + str(count))
#This code is contributed by Jyothi pinjala


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




# initializing tuples
test_tuple = (5, 'Gfg', 2, 8.8, 1.2, 'is')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# initializing data type
data_type = float
 
# Data type frequency in tuple using list comprehension and type()
count = len([x for x in test_tuple if type(x) == data_type])
 
# printing result
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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments