Python 3.x 从python中的名称列表中搜索字符串中的名称

Python 3.x 从python中的名称列表中搜索字符串中的名称,python-3.x,Python 3.x,嗨,我需要搜索字符串中的名称。我的名单是 def get_name(string, list_names): """ Inputs: string: a string to be analyzed list_names: a list of names among whom we are looking for """ for name in list_names: begin_pattern = re.compil

嗨,我需要搜索字符串中的名称。我的名单是

def get_name(string, list_names):
    """
    Inputs:
        string: a string to be analyzed
        list_names: a list of names among whom we are looking for
    """
    for name in list_names:
        begin_pattern = re.compile("^\name")
        if begin_pattern.search(string):
            return name

函数不返回任何错误,但也不返回任何匹配项<代码>获取名称(“达米安·利拉德完成18英尺跳投(艾伦·克拉布助攻)”,列出名称)它应该返回达米安·利拉德。你能帮助我吗。谢谢

编辑:我正在添加符合您需要的代码;忽略我的原始代码

 list_names = ['LaMarcus Aldridge',
     'Damian Lillard',
     'Wesley Matthews',
     'Arron Afflalo',
     'Robin Lopez',
     'Nicolas Batum',
     'Chris Kaman']
达米安·利拉德回来了。或者,如果您想要LaMarcus Aldridge,而不是更改脚本,您可以使用:

def get_name(string,list_names):
    for name in list_names:
        if string.startswith(name):
            print(name)


list_names = ['LaMarcus Aldridge',
 'Damian Lillard',
 'Wesley Matthews',
 'Arron Afflalo',
 'Robin Lopez',
 'Nicolas Batum',
 'Chris Kaman']

get_name("Damian Lillard makes 18-foot jumper (Allen Crabbe assists)",   list_names)
根据你原来的问题,

get_name("LaMarcus Aldridge makes 18-foot jumper (Allen Crabbe assists)", list_names)

编辑:我添加代码的精神,你想要什么;忽略我的原始代码

 list_names = ['LaMarcus Aldridge',
     'Damian Lillard',
     'Wesley Matthews',
     'Arron Afflalo',
     'Robin Lopez',
     'Nicolas Batum',
     'Chris Kaman']
达米安·利拉德回来了。或者,如果您想要LaMarcus Aldridge,而不是更改脚本,您可以使用:

def get_name(string,list_names):
    for name in list_names:
        if string.startswith(name):
            print(name)


list_names = ['LaMarcus Aldridge',
 'Damian Lillard',
 'Wesley Matthews',
 'Arron Afflalo',
 'Robin Lopez',
 'Nicolas Batum',
 'Chris Kaman']

get_name("Damian Lillard makes 18-foot jumper (Allen Crabbe assists)",   list_names)
根据你原来的问题,

get_name("LaMarcus Aldridge makes 18-foot jumper (Allen Crabbe assists)", list_names)
试试这个:

def get_name(string, list_names):
   for player in list_names:
    if player==string:
        return player


list_names = ['LaMarcus Aldridge',
 'Damian Lillard',
 'Wesley Matthews',
 'Arron Afflalo',
 'Robin Lopez',
 'Nicolas Batum',
 'Chris Kaman']

get_name('Damian Lillard', list_names)
试试这个:

def get_name(string, list_names):
   for player in list_names:
    if player==string:
        return player


list_names = ['LaMarcus Aldridge',
 'Damian Lillard',
 'Wesley Matthews',
 'Arron Afflalo',
 'Robin Lopez',
 'Nicolas Batum',
 'Chris Kaman']

get_name('Damian Lillard', list_names)

如果y中有x,则可以使用简单的
,然后检查
y中是否存在
x
。如果字符串包含多个名称,则需要它返回名称列表。可以这样使用列表理解:

import re
def get_name(string, list_names):
    for name in list_names:
        match = re.search(r'\b%s\b'%(name),string)
        if match:
            return name

string ="Damian Lillard makes 18-foot jumper (Allen Crabbe assists)"
list_names = ['LaMarcus Aldridge','Damian Lillard','Wesley Matthews','Arron Afflalo','Robin Lopez','Nicolas Batum','Chris Kaman']

print(get_name(string,list_names))
以下是不使用列表理解的相同函数:

def get_name(string, list_names):
    return [name for name in list_names if name in string]

如果y中有x,则可以使用简单的
,然后检查
y中是否存在
x
。如果字符串包含多个名称,则需要它返回名称列表。可以这样使用列表理解:

import re
def get_name(string, list_names):
    for name in list_names:
        match = re.search(r'\b%s\b'%(name),string)
        if match:
            return name

string ="Damian Lillard makes 18-foot jumper (Allen Crabbe assists)"
list_names = ['LaMarcus Aldridge','Damian Lillard','Wesley Matthews','Arron Afflalo','Robin Lopez','Nicolas Batum','Chris Kaman']

print(get_name(string,list_names))
以下是不使用列表理解的相同函数:

def get_name(string, list_names):
    return [name for name in list_names if name in string]

在您的方法中,不会定义list_players,除非它全局位于您的函数之外。是的,如上所述,我将其全局定义为list_名称。哦,对不起,应该是名单而不是球员名单。我现在编辑了。在你的方法中,没有定义list_players,除非它在你的函数外部全局定义。是的,我在上面的list_名称外部全局定义了它。哦,对不起,应该是名单而不是球员名单。我现在编辑了。我需要在句首搜索名字,而不是名字本身。因此,我需要查看句子的前两个单词是否包含我列表中的名称。
begin\u pattern=re.match(r'\b%s\b'%(player),string)
如果它位于string的开头,则会返回,所以我使用了这个。谢谢大家。我需要在句首搜索名字,而不是名字本身。因此,我需要查看句子的前两个单词是否包含我列表中的名称。
begin\u pattern=re.match(r'\b%s\b'%(player),string)
如果它位于string的开头,则会返回,所以我使用了这个。谢谢大家。我在用spyder。抱歉,此函数不返回任何内容。我做错什么了吗?@user5372470转到站点,选择python 3并将此代码粘贴到编辑器中,然后按run,我已经运行了此代码它返回匹配的名称如果字符串包含列表中的两个名称,它将返回两个名称还是仅返回第一个找到的名称?我想这个问题并没有包含足够的信息,在真正想要的结果上。但是由于有一个名称列表,我假设如果字符串中存在多个名称,那么目标是返回多个名称。我只是注意到另一个答案中的注释,OP似乎只想检查字符串中的前两个单词的名称。所以我想只需要一个名字,即使列表中的两个名字可能存在于同一个字符串中。抱歉,此函数不返回任何内容。我做错什么了吗?@user5372470转到站点,选择python 3并将此代码粘贴到编辑器中,然后按run,我已经运行了此代码它返回匹配的名称如果字符串包含列表中的两个名称,它将返回两个名称还是仅返回第一个找到的名称?我想这个问题并没有包含足够的信息,在真正想要的结果上。但是由于有一个名称列表,我假设如果字符串中存在多个名称,那么目标是返回多个名称。我只是注意到另一个答案中的注释,OP似乎只想检查字符串中的前两个单词的名称。所以我想只需要一个名字,即使列表中的两个名字可能存在于同一个字符串中。