Difference between builder expression and list resolution

Source: Internet
Author: User
Tags generator
Seven, generator expression and list resolution differences Builder expression:

Format: (expr for ITER in iterable [if condition])

The generator is the result of an iteration, not an expression evaluated immediately

Gets a generator object, not a list

Cannot use index, append, etc. list operations

can be converted to list object using the list () factory method

difference between builder and column expressions :

The outermost layer of the builder format is one (), while the outermost list parsing format is a []

The generator is to not read the data once, and the list resolution is to read all the data (memory-consuming)

The generator Returns a generator object (not directly looping), and list resolution returns a new list

------------------------------------------------------

#引入时间工具

Import time

f = open ("E:/pythondata/ods_hbv_addattention")

Start1=time.clock ()

Lines1 = [T.split ("") for T in F]

End1=time.clock ()

Print End1-start1

Print type (lines1)

#文件越大体现越明显

Start2 = Time.clock ()

Lines2 = (T.split (",") for T in F)

End2 = Time.clock ()

Print End2-start2

Print type (lines2)

--------------------------------------------------------------

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.