Python Learning nine: list-generated

Source: Internet
Author: User

List generation is a very powerful expression built into Python that generates a list.

If you want to generate a list [1, 2, 3, 4, 5, 6, 7, 8, 9] You can use range (1, 10):

[python] view plain copy  print?
    1. >>> Range (1, 9)
    2. [1, 2, 3, 4, 5, 6, 7, 8]


But what if you want to build [1*1, 2*2, 3*3, ..., 10*10]? You can use loops:

[python] view plain copy  print?
  1. >>> L= []
  2. >>> for x in range (1, Ten):
  3. ... L.append (x*x)
  4. ...
  5. >>> L
  6. [1, 4, 9, +, (+), +, +, Bayi]


and list generation, you can use a sentence instead of the tedious loop above to complete the above operation:

[python] view plain copy  print?
    1. & Gt;>> print [x*x for x  in range (1 , 11)]  
    2. [1, 4, 9, 16, 25, 36, 49,  64, 81, 100]  
    3. >>>   


List-Generated writing format: [x*x for X in range (1, 11)]

First: Put the elements you want to create x*x to the front

Second: Follow the For loop

This allows the list to be created.

The For loop can also be followed by an if judgment, so that even squares can be filtered out:

[python] view plain copy  print?
    1. >>> [x*x for x in range (1, one ) if x2 = = 0]
    2. [4, +, +,-+ ]
    3. >>>


Of course, you can use a two-layer loop to generate a full array:

[python] view plain copy  print?
  1. >>> < span class= "keyword" >print [m + n for m  in  "ABCD"  for n in  ' XYZ ']  
  2. [' AX ', ' AY ', ' AZ ', ' BX ', ' by ', ' BZ ', ' CX ', ' CY ', ' CZ ', ' DX ', ' DY ', ' DZ ' ']
  3. >>>

How to use two variables to generate a list:

[python] view plain copy  print?
    1. d  = { ' Java ': "the"  ,  ' C ': " ,  ' C + + ': " }  ";
    2. l = [k+ ' = ' +v for k ,  V in d.iteritems ()]  
    3. &NBSP;&NBSP;
    4. print l   


Finally, let's do an exercise:

L = [' Java ', ' C ', ' Swift ', ' Python ', 123], now there is a list containing strings, and integers, the list of uppercase characters into lowercase, pushed out to another list ":

1. Use the built-in isinstance function to determine if a variable is a string

2, S.lower () You can convert one uppercase letter to lowercase.

3. Increase the IF statement to ensure that the list generation is executed correctly.

The code is as follows:

[python] view plain copy  print?
      1. L = [' Java ', ' C ', ' Swift ', ' Python ', 123]
      2. Print [S.lower () if isinstance (S, str) else s for s in L]

Python Learning nine: list-generated

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.