Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep on elements of a list

Tags:

python

grep

list

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!

like image 978
mmann1123 Avatar asked Oct 11 '12 17:10

mmann1123


People also ask

How do you grep a list in Python?

To search a file employing grep in Python, import the “re” package, upload the file, and use a for loop to iterate over each line. On each iteration, use the re.search() method and the RegEx expression as the primary argument and the data line as the second.

How do I grep the contents of a file in Linux?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.

How do you grep words in a file?

To search multiple files with the grep command, insert the filenames you want to search, separated with a space character. The terminal prints the name of every file that contains the matching lines, and the actual lines that include the required string of characters. You can append as many filenames as needed.


2 Answers

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'] 
like image 130
Ashwini Chaudhary Avatar answered Oct 09 '22 23:10

Ashwini Chaudhary


Try this out. It may not be the "shortest" of all the code shown, but for someone trying to learn python, I think it teaches more

names = ['aet2000','ppt2000', 'aet2001', 'ppt2001'] found = [] for name in names:     if 'aet' in name:        found.append(name) print found 

Output

['aet2000', 'aet2001'] 

Edit: Changed to produce list.

See also:

How to use Python to find out the words begin with vowels in a list?

like image 42
mrchampe Avatar answered Oct 10 '22 01:10

mrchampe