VOOZH about

URL: https://www.geeksforgeeks.org/python/python-scoring-matrix-using-dictionary/

⇱ Python - Scoring Matrix using Dictionary - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python - Scoring Matrix using Dictionary

Last Updated : 14 Mar, 2023

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. 

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. 

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.

Comment