Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 查看一个列表中的值是否存在于另一个列表的子字符串中的任何位置_Python_List - Fatal编程技术网

Python 查看一个列表中的值是否存在于另一个列表的子字符串中的任何位置

Python 查看一个列表中的值是否存在于另一个列表的子字符串中的任何位置,python,list,Python,List,因此,我分析了一封HTML电子邮件中的列表,名为br\u list: br_list: [<b>Sent:</b>, <b>To:</b>, <b>Subject:</b>, 'NEFS VII & VIII Manager', 'E-mail: ', 'Office:(508)984-0900 ', 'Cell:(508)965-0064'] 我想看看br_列表中是否包含扇区的条目。这应该很容易 if any(i

因此,我分析了一封HTML电子邮件中的列表,名为
br\u list

br_list: [<b>Sent:</b>, <b>To:</b>, <b>Subject:</b>, 'NEFS VII & VIII Manager', 'E-mail: ', 'Office:(508)984-0900 ', 'Cell:(508)965-0064']
我想看看
br_列表
中是否包含
扇区
的条目。这应该很容易

if any(i in br_list for i in sectors):
print("yup")
……但什么也没印出来。我假设它失败了,因为它正在寻找一个单独的列表条目,这是一个扇区,它不存在,即使一个扇区显然存在于其中一个列表条目中

因此:

1) 是否有办法检查
br\u列表中是否存在这些扇区

2) 如果
扇区
确实存在于
br_列表
中,是否有方法仅捕获该扇区字符串?在这种情况下,
“NEFS VII”


**编辑:**正如前面指出的,我的代码失败是因为
NEFS VII
是列表项的子字符串,而不是列表项本身。我用下面被接受的答案解决了这个问题。

这可能是你想要的,尽管你对这个问题的措辞让很多人(包括我)都不满意。。你想检查我假设的子字符串

br_list = ['NEFS VII & VIII Manager', 'E-mail: ', 'Office:(508)984-0900 ', 'Cell:(508)965-0064']
sectors = (
    'Fixed Gear Sector',
    'Maine Coast Community Sector',
    'Maine Permit Bank',
    'NCCS',
    'NEFS 2',
    'NEFS 3',
    'NEFS 4',
    'NEFS 5',
    'NEFS 6',
    'NEFS 7',
    'NEFS VII',
    'NEFS 8',
    'NEFS VIII',
    'NEFS 9',
    'NEFS 10',
    'NEFS X',
    'NEFS 11',
    'NEFS 12',
    'NEFS 13',
    'New Hampshire Permit Bank',
    'Port Clyde Community Groundfish Sector',
    'Sustainable Harvest Sector 1',
    'Sustainable Harvest Sector 2',
    'Sustainable Harvest Sector 3',
    'Tri-State Sector',
    )

finds = []
for check in sectors:
    if any(check in item for item in br_list):
        finds.append(check)
print(finds)  # ['NEFS VII']

finds = []
for string in br_list:
    finds.extend([x for x in sectors if x in string])
print(finds)

根据这两种方法中哪个更大,两种方法的效率可能会有所不同。

这可能是你想要的,尽管你对这个问题的措辞让很多人(包括我)感到不舒服。。你想检查我假设的子字符串

br_list = ['NEFS VII & VIII Manager', 'E-mail: ', 'Office:(508)984-0900 ', 'Cell:(508)965-0064']
sectors = (
    'Fixed Gear Sector',
    'Maine Coast Community Sector',
    'Maine Permit Bank',
    'NCCS',
    'NEFS 2',
    'NEFS 3',
    'NEFS 4',
    'NEFS 5',
    'NEFS 6',
    'NEFS 7',
    'NEFS VII',
    'NEFS 8',
    'NEFS VIII',
    'NEFS 9',
    'NEFS 10',
    'NEFS X',
    'NEFS 11',
    'NEFS 12',
    'NEFS 13',
    'New Hampshire Permit Bank',
    'Port Clyde Community Groundfish Sector',
    'Sustainable Harvest Sector 1',
    'Sustainable Harvest Sector 2',
    'Sustainable Harvest Sector 3',
    'Tri-State Sector',
    )

finds = []
for check in sectors:
    if any(check in item for item in br_list):
        finds.append(check)
print(finds)  # ['NEFS VII']

finds = []
for string in br_list:
    finds.extend([x for x in sectors if x in string])
print(finds)

根据两种方法中哪个
列表
更大,两种方法的效率可能会有所不同。

首先,您的
扇区
不是
列表
,而是
元组
。您的
br\u列表
包含无效元素(例如
Sent:
可能应加引号)

至于你的第二个问题,你可以做一个嵌套列表理解:

found_sectors = [sector for entry in br_list for sector in sectors if sector in entry]
其结果与:

found_sectors = []
for entry in br_list:
    for sector in sectors:
        if sector in entry:
            found_sectors.append(sector)

首先,您的
扇区
不是一个
列表
,而是一个
元组
。您的
br\u列表
包含无效元素(例如
Sent:
可能应加引号)

至于你的第二个问题,你可以做一个嵌套列表理解:

found_sectors = [sector for entry in br_list for sector in sectors if sector in entry]
其结果与:

found_sectors = []
for entry in br_list:
    for sector in sectors:
        if sector in entry:
            found_sectors.append(sector)

matches=[sector for sector in sectors in sectors if sector in br_list]
nothing的可能重复项与之匹配。。。没有任何东西被打印出来是正常的<代码>'NEFS VII'
br\u列表中作为一个子串存在,而不是作为一个整体存在。@Igle这不是OP想要做的。参见他的第2点。@TheBrowler是您的示例输入太长、太杂乱,而不是您对问题的措辞。如果你说“我有
扇区=(“foo”,“bar”)
输入=[“foo都是food”,“我的酒吧是barred”]
,我需要从
输入中找到的
扇区
中提取任何匹配项,比如
[“foo”,“bar”]
matches=[sector for sector in sectors in sectors if sector in br_list]
可能的副本不匹配,但是…没有打印是正常的。
'NEFS VII'
br\u列表中作为一个子字符串存在,而不是作为一个整体。@Igle这不是OP试图做的。请参阅他的第2点。@浏览者是您的示例输入太长和混乱,而不是您对问题的措辞。如果你说“我有
扇区=(“foo”,“bar”)
输入=[“foo都是food”,“我的酒吧是barred”]
,我需要从
输入中找到的
扇区
中提取任何匹配项,比如
[“foo”,“bar”]
,我们就已经找到了。给我们一堆从屏幕一侧滚动的html标签和一组三十多个可能匹配的元组,掩盖了你想要的。哦,好吧,好吧,我的错,没有正确地表达这个问题。我现在明白为什么我的方法失败了。顺便说一句,这非常有效,谢谢你解释这是我需要搜索的子字符串。我现在明白为什么我的方法失败了。顺便说一句,这非常有效,感谢您解释这是我需要搜索的子字符串。