The title

You are given an array of items where items[I] = [typei, colori, namei] describes the type, color, and name of the ith item.

You are also given a search rule represented by two strings, ruleKey and ruleValue.

Article I is considered to match the given retrieval rule if it satisfies one of the following criteria:

RuleKey == “type” and ruleValue == typei. RuleKey == “color” and ruleValue == colori. RuleKey == “name” and ruleValue == namei. Counts and returns the number of items that match the retrieval rule.

 

Example 1: Enter: items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], ruleKey = "color", RuleValue = "silver" Output: 1 Explanation: Only one item matches the search rules, and that item is ["computer"," Silver "," Lenovo "]. Example 2: Enter: items = [["phone","blue","pixel"],["computer","silver","phone"],["phone","gold","iphone"]], ruleKey = "type", RuleValue = "phone" Output: 2 Explanation: Only two items match the search rules, these are ["phone","blue"," Pixel "] and ["phone","gold"," iPhone "]. Note that ["computer","silver","phone"] did not match the search rule.Copy the code

Tip:

1 <= items.length <= 104 1 <= typei.length, colori.length, namei.length, Rulevalue. length <= 10 ruleKey equals “type”, “color”, or” name” all strings consist of lowercase letters only

Their thinking

class Solution: def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int: type = { "type": 0, "color": 1, "name": 2 } index = type[ruleKey] resList = [i[index] for i in items] return resList.count(ruleValue) if __name__ == '__main__':  # items = [["phone", "blue", "pixel"], ["computer", "silver", "lenovo"], # ["phone", "gold", "iphone"]] # ruleKey = "color" # ruleValue = "silver" items = [["phone", "blue", "pixel"], ["computer", "silver", "phone"], ["phone", "gold", "iphone"]] ruleKey = "type" ruleValue = "phone" ret = Solution().countMatches(items, ruleKey, ruleValue) print(ret)Copy the code