Market = [[1, 'apple', '45'], [2, 'banana', '76'], [3, 'apple', '67']def search(data: List[list], search: str) -> List[int]:"""Return a list of IDs(first index) of fruits whose names contain search"""期望的输出:>>> get_fruits_containing(Market, 'Apple')[1, 3]>>> get_bridges_containing(Market, 'bana')#part of name of fruit[2]"""我尝试过(在市场中搜索 s 中的 s):但没有奏效。它应该接受大写或小写。
1 回答
守着一只汪
TA贡献1872条经验 获得超3个赞
尝试一个函数,如:
def get_fruits_containing(l,i):
return [x[0] for x in l if i.lower() in x[1]]
列表理解是你的朋友:-)。
现在您的查询正在运行:
>>> get_fruits_containing(Market, 'Apple') # Works with uppercase too.
[1, 3]
>>> get_fruits_containing(Market, 'bana')
[2]
添加回答
举报
0/150
提交
取消