Python - Inter Matrix Grouping


The Inter Matrix Grouping is the set of two different lists where the sublist of first element will be considered to be common matches and if the matches are found it makes the first element as key in the dictionary, and the rest two elements are set for matrix grouping. In Python, we have some built−in functions such as setdefault(), defaultdict(), and, append() will be used to solve the Inter Matrix Grouping.

Let’s take an example of this.

The given two lists are:

list_1 = [[1, 2], [3, 4], [4, 5], [5, 6]]

list_2 = [[5, 60], [1, 22], [4, 59], [3, 14]]

Therefore, the final output becomes {1: [2, 22], 3: [4, 14], 4: [5, 59], 5: [6, 60]}

Syntax

The following syntax is used in the examples-

setdefault()

The setdefault is an in−built function in Python that returns the element with a specific key.

append()

The append() is an in−built function in Python that inserts the element at the end of the list.

defaultdict()

The defaultdict() is an in−built function in Python that automatically generates the key along with default value pairs. This function helps to handle the missing key in the dictionary.

Using setdefault() Function

In the following example, the program uses recursive functions to accept two different lists for the calculation of Inter Matrix Grouping. Next, it will use the empty dictionary that will be used to store the final result as an output. Moving ahead to use the for loop that iterates and merges through both the input lists and using built−in functions setdefault() and append() will set the key as a single element and value pairs as matrix grouping. By function return, it generates the result dictionary.

Example

def int_grp_matrix(l1, l2):
    result = {}
    for key, value in l1 + l2:
        result.setdefault(key, []).append(value)
    return result
list1 = [[20, 0], [40, 200], [60, 300]]
list2 = [[40, 500], [60, 50], [20, 100]]
res = int_grp_matrix(list1, list2)
print("Inter Matrix grouping result:\n", res)

Output

 Inter Matrix grouping result: 
{20: [0, 100], 40: [200, 500], 60: [300, 50]}

Using defaultdict() Function

In the following example, we will start the program with a recursive function that calls itself and accepts two parameters− l1 and l2 to receive the value of input lists. Then use the built−in function defaultdict(list) that handles the missing key if the input list of first element doesn’t satisfy for common matches and store it in the variable result. Next, it uses the for loop that iterates and concatenates both the input list. Then the variable result set the parameter as key and for the value pair it will use the built−in function append() to insert the matrix grouping. Finally, we are using function return with the help of built−in function dict() that converts the variable result value into the form of a dictionary.

Example

from collections import defaultdict
def int_grp_matrix(l1, l2):
    result = defaultdict(list)
    for key, value in l1 + l2:
        result[key].append(value)
    return dict(result)
list1 = [[20, 0], [40, 200], [60, 300]]
list2 = [[40, 500], [60, 50], [20, 100]]
res = int_grp_matrix(list1, list2)
print("Inter Matrix grouping result:\n", res)

Output

 Inter Matrix grouping result: 
{20: [0, 100], 40: [200, 500], 60: [300, 50]}

Using a Dictionary and list Comprehension

In the following example, begin the program with a recursive function that sets some of the conditions and operations such as merging the two different lists, empty dictionary, loop, and, conditional statement to calculate the Inter Matrix Grouping and print the output.

Example

def int_grp_matrix(l1, l2):
    merge_list = l1 + l2
    result = {}
    for key, val in merge_list:
        if key in result:
            result[key].append(val)
        else:
            result[key] = [val]
    return result
list1 = [[20, 0], [40, 200], [60, 300]]
list2 = [[40, 500], [60, 50], [20, 100]]
res = int_grp_matrix(list1, list2)
print("The result of Inter Matrix Grouping:\n", res)

Output

The result of Inter Matrix Grouping: {20: [0, 100], 40: [200, 500], 60: [300, 50]}

Conclusion

We know the matrix grouping is the group of integer elements that are set in positions of value pair of the dictionary. The above examples show the initialization of two different lists which means finding common matches of the first element from both lists and transforming this element into a key of the dictionary and setting the value pair as a matrix grouping by using some conditions. This type of program is generally used to solve the Algorithm−problem statement.

Updated on: 14-Aug-2023

54 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements