Thursday, December 26, 2024
Google search engine
HomeLanguagesPython – False values Frequency

Python – False values Frequency

Checking a number/element by a condition is a common problem one faces and is done in almost every program. Sometimes we also require to get the totals that match the particular condition to have a distinguish which to not match for further utilization like in data Science. Lets discuss certain ways in which we can count False values. 

Method #1: Using sum() + generator expression 

This method uses the trick of adding 1 to the sum whenever the generator expression returns true. By the time list gets exhausted, summation of count of numbers matching a condition is returned. 

Python3




# Python 3 code to demonstrate
# False values Frequency
# using sum() + generator expression
 
# initializing list
test_list = [3, False, False, 6, False, 9]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using sum() + generator expression
# False values Frequency
# checks for False
res = sum(1 for i in test_list if not i)
 
# printing result
print ("The number of False elements: " + str(res))


Output

The original list is : [3, False, False, 6, False, 9]
The number of False elements: 3

Time Complexity: O(n) where n is the number of elements in the string list. The sum() + generator expression  is used to perform the task and it takes O(n) time.
Auxiliary Space: O(1) additional space needed

  Method #2: Using sum() + map() 

map() does the task almost similar to the generator expression, difference is just the internal data structure employed by it is different hence more efficient. 

Python3




# Python 3 code to demonstrate
# False values Frequency
# using sum()+ map()
 
# initializing list
test_list = [3, False, False, 6, False, 9]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using sum()+ map()
# False values Frequency
# checks for False
res = sum(map(lambda i: not i, test_list))
 
# printing result
print ("The number of False elements: " + str(res))


Output

The original list is : [3, False, False, 6, False, 9]
The number of False elements: 3

Method #3: Using count() method

This method used count() function to check the count of False values in the list.

Python3




# Python 3 code to demonstrate
# False values Frequency
 
# initializing list
test_list = [3, False, False, 6, False, 9]
 
# printing original list
print ("The original list is : " + str(test_list))
 
res=test_list.count(False)
# printing result
print ("The number of False elements: " + str(res))


Output

The original list is : [3, False, False, 6, False, 9]
The number of False elements: 3

Time Complexity: O(n)
Auxiliary Space: O(n), where n is length of list.

Method #4: Using Counter() method

Python3




# Python 3 code to demonstrate
# False values Frequency
from collections import Counter
# initializing list
test_list = [3, False, False, 6, False, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
freq = Counter(test_list)
falseFreq = freq[False]
# printing result
print("The number of False elements: " + str(falseFreq))


Output

The original list is : [3, False, False, 6, False, 9]
The number of False elements: 3

Time Complexity: O(n)

Auxiliary Space: O(n)

Method #5: Using operator.countOf() method

Python3




# Python 3 code to demonstrate
# False values Frequency
import operator as op
# initializing list
test_list = [3, False, False, 6, False, 9]
 
# printing original list
print ("The original list is : " + str(test_list))
 
res=op.countOf(test_list,False)
# printing result
print ("The number of False elements: " + str(res))


Output

The original list is : [3, False, False, 6, False, 9]
The number of False elements: 3

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #6: Using  list comprehension

Python3




# Method #6: Using list comprehension
 
# initializing list
test_list = [3, False, False, 6, False, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using list comprehension
res = len([i for i in test_list if not i])
 
# printing result
print("The number of False elements: " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list is : [3, False, False, 6, False, 9]
The number of False elements: 3

# Time Complexity: O(n)
# Auxiliary Space: O(n)
 Method #7: Using reduce method.

Algorithm:

1. The `reduce()` function takes two arguments: a function and an iterable.
2. The function argument must take two arguments: an accumulator and an element from the iterable.
3. The `reduce()` function applies the function to the first two elements of the iterable and saves the result as the accumulator.
4. It then applies the function to the accumulator and the next element in the iterable, saving the result as the new accumulator.
5. This process continues until all elements of the iterable have been processed.
6. The final result of the `reduce()` function is the final value of the accumulator.

Python3




from functools import reduce
 
# initializing list
test_list = [3, False, False, 6, False, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using reduce method
res = reduce(lambda acc, x: acc + 1 if x == False else acc, test_list, 0)
 
# printing result
print("The number of False elements: " + str(res))


Output

The original list is : [3, False, False, 6, False, 9]
The number of False elements: 3

Time Complexity:
– In the worst case, the `reduce()` method must iterate over all elements in the iterable, so the time complexity is O(n), where n is the number of elements in the iterable.
– However, the time complexity of the function argument passed to `reduce()` can vary, depending on the complexity of the calculation being performed.

Space Complexity:
– The `reduce()` method requires constant space overhead, regardless of the size of the iterable.
– The space complexity of the function argument passed to `reduce()` can also vary, depending on the complexity of the calculation being performed.

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