且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python:列表中的完整字符串与部分字符串的交集

更新时间:2023-11-15 14:38:28

如果只希望b中的第一个匹配项:

If you only want the first match in b:

next((s for s in b if s in a), None)

这样做的好处是,一旦找到匹配项,就会将其短路,而其他列表解决方案将继续保持下去.如果找不到匹配项,它将返回None.

This has the advantage of short-circuiting as soon as it finds a match whereas the other list solutions will keep going. If no match is found, it will return None.