[英]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.