
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to count items matching a rule using Python
Suppose we have an array nums, where each nums[i] contains three elements [type_i, color_i, name_i]. These are describing the type, color, and name of the ith item. We also have a rule represented by two other strings, ruleKey and ruleValue. Now we can say the ith item is matched the rule if one of the following is true −
ruleKey = "type" and ruleValue = type_i.
ruleKey = "color" and ruleValue = color_i.
ruleKey = "name" and ruleValue = name_i.
We have to find number of matching we can find.
So, if the input is like
Bike | blue | ElecB |
Car | silver | Sumo |
Bike | blue | TVS |
And ruleKey = "color", ruleValue = "blue", then the output will be 2 as there are two match [["Bike","blue","ElecB"] and ["Bike","blue","TVS"]].
To solve this, we will follow these steps −
count := 0
if ruleKey is same as "type", then
for i in range 0 to size of items, do
if items[i, 0] is same as ruleValue, then
count := count + 1
if ruleKey is same as "color", then
for i in range 0 to size of items, do
if items[i, 1] is same as ruleValue, then
count := count + 1
if ruleKey is same as "name", then
for i in range 0 to size of items, do
if items[i, 2] is same as ruleValue, then
count := count + 1
return count
Let us see the following implementation to get better understanding −
Example
def solve(items, ruleKey, ruleValue): count = 0 if ruleKey == "type": for i in range(len(items)): if items[i][0] == ruleValue: count += 1 if ruleKey == "color": for i in range(len(items)): if items[i][1] == ruleValue: count += 1 if ruleKey == "name": for i in range(len(items)): if items[i][2] == ruleValue: count += 1 return count items = [["Bike","blue","ElecB"],["Car","silver","Sumo"],["Bike","blue","TVS"]] ruleKey = "color" ruleValue = "blue" print(solve(items, ruleKey, ruleValue))
Input
[["Bike","blue","ElecB"],["Car","silver","Sumo"],["Bike","blue","TVS"]],"color", "blue"
Output
2
- Related Articles
- Program to count good meals with exactly two items in Python
- Count of elements matching particular condition in Python
- Matching MongoDB collection items by id?
- C++ Program to Implement String Matching Using Vectors
- Template matching using OpenCV in Python
- Program to count submatrices with all ones using Python
- C++ Program to Perform String Matching Using String Library
- Program to count average of all special values for all permutations of a list of items in Python
- Count the Number of matching characters in a pair of string in Python
- Python Program to remove items from set
- Prefix matching in Python using pytrie module
- Python Program to Multiply All the Items in a Dictionary
- Python program to count number of vowels using set in a given string
- Program to count odd numbers in an interval range using Python
- Python Program to remove items from the set
