如果给定的字符串在任何子列表中,则返回子列表的第一个索引

时间:2018-11-02 01:19:12

标签: python search

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]
"""

我尝试过 如果有的话(在Market中在中搜索,但没有用)。应该接受大写或小写。

1 个答案:

答案 0 :(得分:0)

尝试类似的功能

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]
相关问题