VOOZH about

URL: https://www.geeksforgeeks.org/python/python-factors-frequency-dictionary/

⇱ Python - Factors Frequency Dictionary - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python - Factors Frequency Dictionary

Last Updated : 4 May, 2023

Given a list with elements, construct a dictionary with frequency of factors.

Input : test_list = [2, 4, 6, 8] Output : {1: 4, 2: 4, 3: 1, 4: 2, 5: 0, 6: 1, 7: 0, 8: 1} Explanation : All factors count mapped, e.g 2 is divisible by all 4 values, hence mapped with 4. Input : test_list = [1, 2] Output : {1: 2, 2 : 1} Explanation : Similar as above, 1 is factor of all.

Method #1 : Using loop 

 This is brute way in which this task can be performed. In this, the elements are iterated and required number is checked for being a factor, if yes, its frequency is increased in dictionary corresponding to its key.


Output
The original list : [2, 4, 6, 8, 3, 9, 12, 15, 16, 18]
The constructed dictionary : {1: 10, 2: 7, 3: 6, 4: 4, 5: 1, 6: 3, 7: 0, 8: 2, 9: 2, 10: 0, 11: 0, 12: 1, 13: 0, 14: 0, 15: 1, 16: 1, 17: 0}

Time Complexity: O(n*n), where n is the elements of dictionary
Auxiliary Space: O(n), where n is the size of dictionary

Method #2 : Using sum() + loop

This is almost similar approach to above problem. The difference being sum() is used for summation rather than a manual loop for solving problem. 


Output
The original list : [2, 4, 6, 8, 3, 9, 12, 15, 16, 18]
The constructed dictionary : {1: 10, 2: 7, 3: 6, 4: 4, 5: 1, 6: 3, 7: 0, 8: 2, 9: 2, 10: 0, 11: 0, 12: 1, 13: 0, 14: 0, 15: 1, 16: 1, 17: 0}

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. 
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”. 

Method #3: Using collections.Counter() and itertools.chain()

Import the collections module.
Initialize a dictionary res with all the keys as integers from 1 to the maximum value in the test_list.
Convert the test_list into a list of factors using a nested list comprehension.
Flatten the list of factors into a single list using the itertools.chain() method.
Count the frequency of each factor using the collections.Counter() method and store the result in res.
Print the resulting dictionary res.


Output
The original list : [2, 4, 6, 8, 3, 9, 12, 15, 16, 18]
The constructed dictionary : {1: 10, 2: 7, 3: 6, 4: 4, 5: 1, 6: 3, 7: 0, 8: 2, 9: 2, 10: 0, 11: 0, 12: 1, 13: 0, 14: 0, 15: 1, 16: 1, 17: 0, 18: 1}

The time complexity O(n^2), where n is the length of the test_list.
 The auxiliary space  O(n^2), since we are creating a list of factors for each element in test_list, and then flattening it into a single list.

Method #4 : Using list(),set() and count() methods

Approach

  1. Find the factors of each number of test_list using nested for loops and append them to an empty list x
  2. Remove the duplicates from x using list(),set() and store it in y, create an empty dictionary res
  3. Initiate a for loop over list y and initialise the dictionary with elements of y as keys and count of these elements in x as values
  4. Display res

Output
The original list : [2, 4, 6, 8, 3, 9, 12, 15, 16, 18]
The constructed dictionary : {1: 10, 2: 7, 3: 6, 4: 4, 5: 1, 6: 3, 8: 2, 9: 2, 12: 1, 15: 1, 16: 1}

Time Complexity : O(M*N) M - length of range 1 to max(test_list) N - length of test_list

Auxiliary Space : O(N) N - length of res dictionary

Comment