简体   繁体   English

使用每个项目中包含的特定值在项目列表中使用过滤

[英]using filtering in list of items using specific values contain in each item

I have a list for example我有一个清单,例如

list = ["1234doy001.tif", "1233doy001.tif", "1111doy002.tif", "2222doy002.tif"]

I would like to filter the list that it will only give me list with values having doy001.我想过滤列表,它只会给我包含 doy001 的值的列表。

desired result:想要的结果:

filter = ["1234doy001.tif", "1233doy001.tif"]

I am trying我在尝试

filter = [j for j in list if j == '*doy001*']

or或者

filter = [j for j in list if j == 'doy001']

but with no success.但没有成功。 It just returns an empty list, like []它只返回一个空列表,如 []

Please advise.请指教。

Thanks, -Leo谢谢,-Leo

j == '*doy001*' checks if j is equal to *doy001* j == '*doy001*'检查j是否等于*doy001*

j == 'doy001' checks if j is equal to doy001 j == 'doy001'检查j是否等于doy001

These approaches are both wrong.这两种方法都是错误的。

Use the in operator;使用in运算符;

filter = [j for j in list if 'doy001' in j]

使用字符串in您可以检查一个字符串是否包含在另一个字符串中

filter = [j for j in list if 'doy001' in j]

You can use :您可以使用 :

filter =   [j for j in l if 'doy001' in j]

also

filter = [j for j in l if (j.find('doy001')!= -1)]

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 itemgetter 和 list comp 过滤元组列表,以便根据元组中的第二项获取唯一项 - Filtering list of tuples using itemgetter and list comp in order to get unique items based on the second item in the tuple 使用下拉列表过滤项目 - Filtering items using drop down list 按多个项目排序一个dicts列表,为每个项目排序不同的反向值 - Sort a list of dicts by multiple items AND different reverse values for each item 使用python中的值过滤字典列表 - Filtering a list of dictionaries using its values in python 为列表中的每个项目添加换行符,使用 beautifulsoup 找到项目 - Add line break for each item in list,items were found by using beautifulsoup 使用Python中的嵌套列表推导过滤掉列表中的项目 - Filtering out items from a list using nested list comprehensions in Python 如何在不使用.pop()或.remove()的情况下从项目列表中删除特定项目的第一次出现 - How to remove first occurrence of a specific item from a list of items without using .pop() or .remove() 使用列表作为格式字符串的值,而不必分别列出每个项目 - using a list as values to a format string without having to list each item separately 使用特定标准的列表中项目的组合 - Combinations of items in a list using specific critera 使用pop从列表中删除特定项目 - Deleting specific items from list using pop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM