Grep on elements of a list

grep, list, python

Solution

Use `filter()`:

>>> names = ['aet2000','ppt2000', 'aet2001', 'ppt2001']
>>> filter(lambda x:'aet' in x, names)
['aet2000', 'aet2001']

with `regex`:

>>> import re
>>> filter(lambda x: re.search(r'aet', x), names)
['aet2000', 'aet2001']

In Python 3 filter returns an iterator, hence to get a list call `list()` on it.

>>> list(filter(lambda x:'aet' in x, names))
['aet2000', 'aet2001']

else use list-comprehension(it will work in both Python 2 and 3:

>>> [name for name in names if 'aet' in name]
['aet2000', 'aet2001']

Problem

I have a list of files names: ``` names = ['aet2000','ppt2000', 'aet2001', 'ppt2001'] ``` While I have found some functions that can work to grep character strings, I haven't figured out how to grep all elements of a list. for instance I would like to: ``` grep(names,'aet') ``` and get: ``` ['aet2000','aet2001'] ``` Sure its not too hard, but I am new to Python update The question above apparently wasn't accurate enough. All the answers below work for the example but not for my actual data. Here is my code to make the list of file names: ``` years = range(2000,2011) months = ["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"] variables = ["cwd","ppt","aet","pet","tmn","tmx"] # *variable name* with wildcards tifnames = list(range(0,(len(years)*len(months)*len(variables)+1) )) i = 0 for variable in variables: for year in years: for month in months: fullname = str(variable)+str(year)+str(month)+".tif" tifnames[i] = fullname i = i+1 ``` Running filter(lambda x:'aet' in x,tifnames) or the other answers return: ``` Traceback (most recent call last): File "<pyshell#89>", line 1, in <module> func(tifnames,'aet') File "<pyshell#88>", line 2, in func return [i for i in l if s in i] TypeError: argument of type 'int' is not iterable ``` Despite the fact that tifnames is a list of character strings: ``` type(tifnames[1]) <type 'str'> ``` Do you guys see what's going on here? Thanks again!

Original source

Related problems