确定字符串是否包含多个单词?

时间:2014-10-10 03:45:24

标签: python

例如:

def matching_names(name1, name2):

""" (list of str, str) -> list of str

和name1的格式为' firstname lastname', 如果我希望它返回name1等于name2的name1列表,我该怎么办?

离)

matching_names(['sarah james', 'rebecca black', 'rebecca kim'], 'rebecca')
  
    
      

[' rebecca black',' rebecca kim']

    
  

2 个答案:

答案 0 :(得分:0)

def matching_names(name1, name2):
    a = []
    for name in name1:
        if name.split(" ")[0] == name2:
            a.append(name)
    return a

答案 1 :(得分:0)

results = []
for entry in name1:
  if entry.split()[0] == name2:
    results.append(entry)

return results