简体   繁体   中英

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.

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

j == '*doy001*' checks if j is equal to *doy001*

j == 'doy001' checks if j is equal to doy001

These approaches are both wrong.

Use the in operator;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM