在嵌套列表中搜索单词

时间:2014-10-10 19:29:59

标签: python list search

我正在尝试编写一个在列表中搜索特定内容的函数:

def search(inlist, matches):
    for li in inlist:
        for m in matches:
            if m in li:
                return li
    return none

l = [("daniel", "20th november", "tenochtitlan"), ("Arturo", 17, "17th october")]

例如,在这里寻找丹尼尔的出生日期。

我有这个来定义搜索,但我不确定如何从这里开始。

1 个答案:

答案 0 :(得分:-1)

从写的代码我得到了这个:

l = [("daniel", "20th november", "tenochtitlan"), ("Arturo", 17, "17th october")]
data = search(l, ['daniel'])
#data will be ("daniel", "20th november", "tenochtitlan")
for d in data:
   #regexp to find something specific, return that value
   if SomeRegexp.match(d):
      return d

但如果你想要更具体的答案,你必须更加具体。