Given dual tuples, get a count of unique keys for each value present in the tuple.
Input : test_list = [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2), (8, 1), (9, 1), (8, 4), (10, 4)]
Output : {4: 4, 2: 3, 1: 2}
Explanation : 3, 2, 8 and 10 are keys for value 4.
Input : test_list = [(3, 4), (1, 2), (8, 1), (9, 1), (8, 4), (10, 4)]
Output : {4: 3, 2: 1, 1: 2}
Explanation : 3, 8 and 10 are keys for value 4.
Method #1 : Using loop + defaultdict()
In this, we iterate for each tuple element, having key as tuple value, and increment it with each different key encountered in the dictionary value list. Next, frequency is computed using another iteration by getting length of mapped value list, converting to set to get a unique count.
Python3
from collections import defaultdict
test_list = [( 3 , 4 ), ( 1 , 2 ), ( 2 , 4 ), ( 8 , 2 ), ( 7 , 2 ), ( 8 , 1 ), ( 9 , 1 ), ( 8 , 4 ), ( 10 , 4 )]
print ( "The original list is : " + str (test_list))
res = defaultdict( list )
for sub in test_list:
res[sub[ 1 ]].append(sub[ 0 ])
res = dict (res)
res_dict = dict ()
for key in res:
res_dict[key] = len ( list ( set (res[key])))
print ( "Unique keys for values : " + str (res_dict))
|
Output
The original list is : [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2), (8, 1), (9, 1), (8, 4), (10, 4)]
Unique keys for values : {4: 4, 2: 3, 1: 2}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using loop + defaultdict() + Counter()
In this, in order to reduce a computation loop, the list is constructed dynamically having just unique values. Then Counter() is used to get unique value dictionary.
Python3
from collections import defaultdict, Counter
test_list = [( 3 , 4 ), ( 1 , 2 ), ( 2 , 4 ), ( 8 , 2 ), ( 7 , 2 ),
( 8 , 1 ), ( 9 , 1 ), ( 8 , 4 ), ( 10 , 4 )]
print ( "The original list is : " + str (test_list))
mem_dict = defaultdict( list )
res = []
for sub in test_list:
if sub[ 0 ] not in mem_dict[sub[ 1 ]]:
mem_dict[sub[ 1 ]].append(sub[ 0 ])
res.append(sub[ 1 ])
res = dict (Counter(res))
print ( "Unique keys for values : " + str (res))
|
Output
The original list is : [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2), (8, 1), (9, 1), (8, 4), (10, 4)]
Unique keys for values : {4: 4, 2: 3, 1: 2}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using count() method
Python3
test_list = [( 3 , 4 ), ( 1 , 2 ), ( 2 , 4 ), ( 8 , 2 ), ( 7 , 2 ), ( 8 , 1 ), ( 9 , 1 ), ( 8 , 4 ), ( 10 , 4 )]
print ( "The original list is : " + str (test_list))
x = []
for i in test_list:
i = list (i)
x.append(i[ 1 ])
d = dict ()
for i in x:
d[i] = x.count(i)
print ( "Unique keys for values : " + str (d))
|
Output
The original list is : [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2), (8, 1), (9, 1), (8, 4), (10, 4)]
Unique keys for values : {4: 4, 2: 3, 1: 2}
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) additional space of size n is created
Method 4: using operator.countOf() method
Python3
import operator as op
test_list = [( 3 , 4 ), ( 1 , 2 ), ( 2 , 4 ), ( 8 , 2 ), ( 7 , 2 ),
( 8 , 1 ), ( 9 , 1 ), ( 8 , 4 ), ( 10 , 4 )]
print ( "The original list is : " + str (test_list))
x = []
for i in test_list:
i = list (i)
x.append(i[ 1 ])
d = dict ()
for i in x:
d[i] = op.countOf(x, i)
print ( "Unique keys for values : " + str (d))
|
Output
The original list is : [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2), (8, 1), (9, 1), (8, 4), (10, 4)]
Unique keys for values : {4: 4, 2: 3, 1: 2}
Time Complexity: O(N)
Auxiliary Space : O(N)
Method 5: Using numpy:
Algorithms :
- Initialize an empty list x.
- Iterate through each tuple in test_list.
- Convert the tuple to a list and append the second element of the list to x.
- Initialize an empty dictionary d.
- Iterate through each element i in x.
- Add a key-value pair to d where the key is i and the value is the number of times i appears in x.
- Print the resulting dictionary d.has context menu
Python3
import numpy as np
test_list = [( 3 , 4 ), ( 1 , 2 ), ( 2 , 4 ), ( 8 , 2 ), ( 7 , 2 ),
( 8 , 1 ), ( 9 , 1 ), ( 8 , 4 ), ( 10 , 4 )]
print ( "The original list is : " + str (test_list))
arr = np.array(test_list)
col = arr[:, 1 ]
unique, counts = np.unique(col, return_counts = True )
unique_keys_for_values = dict ( zip (unique, counts))
print ( "Unique keys for values : " + str (unique_keys_for_values))
|
Output:
The original list is : [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2), (8, 1), (9, 1), (8, 4), (10, 4)]
Unique keys for values : {1: 2, 2: 3, 4: 4}
The time complexity :O(n), where n is the length of the input list. This is because the code iterates through the list once to extract the second element of each tuple and then again to count the number of occurrences of each second element.
The space complexity :O(k), where k is the number of unique second elements in the input list. This is because the code creates a dictionary to store the count of each unique second element, which has a space complexity proportional to the number of unique second elements.
Method 6: Using dictionary comprehension + set comprehension
Python3
from collections import defaultdict
test_list = [( 3 , 4 ), ( 1 , 2 ), ( 2 , 4 ), ( 8 , 2 ), ( 7 , 2 ), ( 8 , 1 ), ( 9 , 1 ), ( 8 , 4 ), ( 10 , 4 )]
print ( "The original list is : " + str (test_list))
res_dict = {sub[ 1 ]: len ({x[ 0 ] for x in test_list if x[ 1 ] = = sub[ 1 ]}) for sub in test_list}
print ( "Unique keys for values : " + str (res_dict))
|
Output
The original list is : [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2), (8, 1), (9, 1), (8, 4), (10, 4)]
Unique keys for values : {4: 4, 2: 3, 1: 2}
Time complexity: O(n^2)
Auxiliary space: O(n)
Share your thoughts in the comments
Please Login to comment...