Open In App

Python – Scoring Matrix using Dictionary

Last Updated : 14 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python records, we can have a problem in which we need to resolve scoring in Python Matrix records. This means mapping of each key of dictionary with its value to aggregate score of each row. This kind of problem can have applications in gaming and web development domains. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [[‘gfg’, ‘best’], [‘geeks’], [‘is’, ‘for’]] Output : [18, 15, 12] Input : test_list = [[‘gfg’, ‘geeks’, ‘CS’]] Output : [20]

Method #1 : Using loop This is one of the way in which this task can be performed. In this, we iterate for the matrix elements and perform the values substitutions using dictionary and perform row summations. 

Python3




# Python3 code to demonstrate working of
# Scoring Matrix using Dictionary
# Using loop
 
# initializing list
test_list = [['gfg', 'is', 'best'], ['gfg', 'is', 'for', 'geeks']]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing test dict
test_dict = {'gfg' : 5, 'is' : 10, 'best' : 13, 'for' : 2, 'geeks' : 15}
 
# Scoring Matrix using Dictionary
# Using loop
res = []
for sub in test_list:
    sum = 0
    for val in sub:
        if val in test_dict:
            sum += test_dict[val]
    res.append(sum)
 
# printing result
print("The Row scores : " + str(res))


Output : 

The original list is : [['gfg', 'is', 'best'], ['gfg', 'is', 'for', 'geeks']]
The Row scores : [28, 32]

Time complexity: O(M^N) as the number of combinations generated is M choose N.

Auxiliary space: O(L) as the size of the resultant list is L.

  Method #2 : Using list comprehension + sum() This is yet another way to solve this problem. In this, we perform the summation using sum() and list comprehension is used for iterations and score assignments. 

Python3




# Python3 code to demonstrate working of
# Scoring Matrix using Dictionary
# Using list comprehension + sum()
 
# initializing list
test_list = [['gfg', 'is', 'best'], ['gfg', 'is', 'for', 'geeks']]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing test dict
test_dict = {'gfg' : 5, 'is' : 10, 'best' : 13, 'for' : 2, 'geeks' : 15}
 
# Scoring Matrix using Dictionary
# Using list comprehension + sum()
res = [sum(test_dict[word] if word.lower() in test_dict else 0 for word in sub) for sub in test_list]
 
# printing result
print("The Row scores : " + str(res))


Output : 

The original list is : [['gfg', 'is', 'best'], ['gfg', 'is', 'for', 'geeks']]
The Row scores : [28, 32]

Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(L) as the size of the resultant list is L.



Similar Reads

Python - Maximum Scoring word
Given a String, the task is to write a Python program to compute maximum scoring words i.e words made of characters with a maximum positional summation. Examples: Input : test_str = 'geeks must use geeksforgeeks for cs knowledge'Output : geeksforgeeksExplanation : Sum of characters positional values for "geeksforgeeks" word is maximum, hence result
5 min read
Python | Convert flattened dictionary into nested dictionary
Given a flattened dictionary, the task is to convert that dictionary into a nested dictionary where keys are needed to be split at '_' considering where nested dictionary will be started. Method #1: Using Naive Approach Step-by-step approach : Define a function named insert that takes two parameters, a dictionary (dct) and a list (lst). This functi
8 min read
Python | Convert nested dictionary into flattened dictionary
Given a nested dictionary, the task is to convert this dictionary into a flattened dictionary where the key is separated by '_' in case of the nested key to be started. Method #1: Using Naive Approach Step-by-step approach : The function checks if the input dd is a dictionary. If it is, then it iterates over each key-value pair in the dictionary, a
8 min read
Convert String Dictionary to Dictionary Python
Interconversions of data types have been discussed many times and have been quite a popular problem to solve. This article discusses yet another problem of interconversion of the dictionary, in string format to a dictionary. Let's discuss certain ways in which this can be done. Convert String Dictionary to Dictionary Using json.loads() This task ca
6 min read
Python | Dictionary initialization with common dictionary
Sometimes, while working with dictionaries, we might have an utility in which we need to initialize a dictionary with records values, so that they can be altered later. This kind of application can occur in cases of memoizations in general or competitive programming. Let’s discuss certain way in which this task can be performed. Method 1: Using zip
7 min read
Python - Update dictionary with other dictionary
Sometimes, while working with Python dictionaries, we can have problem in which we need to perform the update of dictionary with other keys of dictionary. This can have applications in domains in which we need to add certain records to previously captured records. Let's discuss certain ways in which this task can be performed. Method #1 : Using loo
9 min read
Convert Dictionary Value list to Dictionary List Python
Sometimes, while working with Python Dictionaries, we can have a problem in which we need to convert dictionary list to nested records dictionary taking each index of dictionary list value and flattening it. This kind of problem can have application in many domains. Let's discuss certain ways in which this task can be performed. Input : test_list =
9 min read
Python - Replace dictionary value from other dictionary
Given two dictionaries, update the values from other dictionary if key is present in other dictionary. Input : test_dict = {"Gfg" : 5, "is" : 8, "Best" : 10, "for" : 8, "Geeks" : 9}, updict = {"Geeks" : 10, "Best" : 17} Output : {'Gfg': 5, 'is': 8, 'Best': 17, 'for': 8, 'Geeks': 10} Explanation : "Geeks" and "Best" values updated to 10 and 17. Inpu
6 min read
Python - Append Dictionary Keys and Values ( In order ) in dictionary
Given a dictionary, perform append of keys followed by values in list. Input : test_dict = {"Gfg" : 1, "is" : 2, "Best" : 3} Output : ['Gfg', 'is', 'Best', 1, 2, 3] Explanation : All the keys before all the values in list. Input : test_dict = {"Gfg" : 1, "Best" : 3} Output : ['Gfg', 'Best', 1, 3] Explanation : All the keys before all the values in
5 min read
Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
Given two dictionaries. The task is to merge them in such a way that the resulting dictionary contains the key from the first dictionary and the value from the second dictionary. Examples: Input : test_dict1 = {"Gfg" : 20, "is" : 36, "best" : 100}, test_dict2 = {"Gfg2" : 26, "is2" : 20, "best2" : 70} Output : {'Gfg': 26, 'is': 20, 'best': 70} Expla
8 min read
three90RightbarBannerImg