"Python Cookbook" "Data Structure and algorithm" 19. Simultaneous conversion and conversion of data

Source: Internet
Author: User

Problem: We need to invoke a conversion function (for example, sum (), Min (), Max ()), but first you need to convert or filter the data

Solution: Very elegant method---use builder expressions in function arguments

For example:

#calculate the sum of squaresnums=[1,2,3,4,5]s1=sum ((x*x forXinchnums)) S2=sum (x*x forXinchNums#more elegant usage.S3=sum ([x*x forXinchNums])#do not use builder expressionsPrint(S1)Print(S2)Print(S3)#determine if a. py file exists under a directoryImportOsfiles= Os.listdir (Os.path.expanduser ('~'))Print(Files)ifAny (Name.endswith ('. PY') forNameinchfiles):Print('there be python!')Else:    Print('Sorry, no python.')#Output a tuple as CSVs = ('ACME', 50, 123.45)Print(','. Join (STR (x) forXinchs))#data reduction across fields of a data structurePortfolio = [   {'name':'GOOG','shares': 50},   {'name':'YHOO','shares': 75},   {'name':'AOL','shares': 20},   {'name':'Scox','shares': 65}]min_shares= Min (s['shares'] forSinchportfolio)Print(min_shares) Min_shares2= Min (portfolio,key=Lambdas:s['shares'])#Using builder ExpressionsPrint(Min_shares2)
>>> ================================ RESTART ================================>>> 555555['. Idlerc','. Oracle_jre_usage','AppData','Application Data','Contacts','Cookies','Desktop','Documents','Downloads','Favorites','HelloWorld','Helloworld.zip','Links','Local Settings','log.html','Music','My Documents','MySite','Mysite.zip','NetHood','NTUSER. DAT','ntuser.dat.LOG1','ntuser.dat.LOG2','NTUSER. dat{6cced2f1-6e01-11de-8bed-001e0bcd1824}. TM.BLF','NTUSER. dat{6cced2f1-6e01-11de-8bed-001e0bcd1824}. Tmcontainer00000000000000000001.regtrans-ms','NTUSER. dat{6cced2f1-6e01-11de-8bed-001e0bcd1824}. Tmcontainer00000000000000000002.regtrans-ms','Ntuser.ini','Output.xml','Pictures','Pip','PrintHood','recent','report.html','Saved Games','Searches','SendTo','Templates','Videos','Start Menu']sorry, no python. ACME,50,123.4520{'shares': 20,'name':'AOL'}>>>

Summarize:

This scenario shows some of the grammatical subtleties of using a generator expression as a function parameter (that is, you do not have to reuse parentheses), for example, the following two lines of code represent the same meaning:

 for inch nums)) s for in nums)  # more Elegant usage
S3=sum ([x*x for X in Nums]) #不使用生成器表达式

It is more efficient and elegant to use generators to make parameters than to create a temporary list first.

"Python Cookbook" "Data Structure and algorithm" 19. Simultaneous conversion and conversion of data

Related Article

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.