python - Count the number of list elements that does not include certain strings -



python - Count the number of list elements that does not include certain strings -

i have big text file containing 900000 lines. have count lines not have 'year1995' , 'year1996' in line. did follows:

fname = r"data.txt" open(fname,'r') fi: lines = fi.read().splitlines() print len(lines) test = [l l in lines if 'year1995' or 'year1996' not in l] print len(test)

but code not producing expected result.

any ideas?

the code have there set every single line in test. because first if statement evaluate true because non-empty strings truthy. alter test within comprehension:

[l l in lines if not ('year1995' in l or 'year1996' in l)]

python

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -