给你一个数组 items
,其中 items[i] = [typei, colori, namei]
,描述第 i
件物品的类型、颜色以及名称。
另给你一条由两个字符串 ruleKey
和 ruleValue
表示的检索规则。
如果第 i
件物品能满足下述条件之一,则认为该物品与给定的检索规则 匹配 :
ruleKey == "type"
且ruleValue == typei
。ruleKey == "color"
且ruleValue == colori
。ruleKey == "name"
且ruleValue == namei
。
统计并返回 匹配检索规则的物品数量 。
1773. 统计匹配检索规则的物品数量 – 力扣(Leetcode)

思路:
读懂就行, 题意是找到键对应值匹配的数量。
python3实现:
class Solution:
def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
# 题意是找到键对应值匹配的数量
type_list = []
color_list = []
name_list = []
for elem in items:
type_list.append(elem[0])
color_list.append(elem[1])
name_list.append(elem[2])
# 定位idx
if ruleKey == "type":
if ruleValue in type_list:
return type_list.count(ruleValue)
else:
return 0
if ruleKey == "color":
if ruleValue in color_list:
return color_list.count(ruleValue)
else:
return 0
if ruleKey == "name":
if ruleValue in name_list:
return name_list.count(ruleValue)
else:
return 0
