Python: Calculate the length of the longest row in a large file efficiently

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.