When operating on large files frequently used by many processes, you should release the file resources (f. close () as soon as possible ())
The first two methods are mainly usedList Parsing, Performance is slightly poor, and the last useGenerator expressionSaves more memory than list Parsing
List Parsing is similar to generator expressions:
List Parsing
[Expr for iter_var in iterable if cond_expr]
Generator expression
(Expr for iter_var in iterable if cond_expr)
Method 1: original
longest = 0f = open(FILE_PATH,"r")allLines = [line.strip() for line in f.readlines()]f.close()for line in allLines: linelen = len(line) if linelen>longest: longest = linelen
Method 2: Concise
f = open(FILE_PATH,"r")allLineLens = [len(line.strip()) for line in f]longest = max(allLineLens)f.close()
Disadvantage: when one row iterates f, the list parsing needs to read all the lines of the file into the memory, and then generate a list
Method 3: The simplest and most memory-saving
f = open(FILE_PATH,"r")longest = max(len(line) for line in f)f.close()
Or
print max(len(line.strip()) for line in open(FILE_PATH))
Reference: Python core programming (Chapter 8th) original address: was a civil reprint please indicate the source: http://www.cnblogs.com/hongfei/p/3768207.html