如何检查列表中字符串的一部分? [英] How to check for part of a string within a list?

查看:57
本文介绍了如何检查列表中字符串的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查列表中字符串的最有效方法是什么?

What is the most efficient way to check for part of a string within a list?

例如,说我在以下列表中寻找"4110964_se"(真)或"4210911_sw"(假):

For example, say I am looking for "4110964_se" (True) or "4210911_sw" (False) in the following list:

files = ['H:\\co_1m_2013\\41108\\m_4110864_se_12_1_20130717.tif',
         'H:\\co_1m_2013\\41108\\m_4110864_sw_12_1_20130717.tif',
         'H:\\co_1m_2013\\41109\\m_4110964_se_12_1_20130722.tif']

如果我使用简单的检查,结果将不是我期望的:

If I use a simple check, the results are not what I would expect:

>>> "4110964_se" in files
False

推荐答案

any('4110964_se' in f for f in files) # check if the string is in any of the list items

这篇关于如何检查列表中字符串的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆