Powerful and elegant list derivation expressions in Python

Source: Internet
Author: User
Tags for in range stdin

The derivation of the expression is actually to simplify some circular judgment operation, etc.

How many ways can you generate a list of number 1-10?

>>> L = [1, 2, 3, 4, 5, 6, 7, 8, 9, ten ]>>> l[1, 2, 3, 4, 5, 6, 7, 8, 9, ten]
>>> L = [] for in range (1, one ): ...      . >>> l[1, 2, 3, 4, 5, 6, 7, 8, 9, >>>
>>> L = Range (1, one )>>> l[1, 2, 3, 4, 5, 6, 7, 8, 9, ten]

Now look at the derivation expression

 for  in range (1, one )]>>> a[1, 2, 3, 4, 5, 6, 7, 8, 9, ten]

Some people, may say, direct range (1, 11) just fine, superfluous, if we're going to sift through the odd number?

Of course, range can still do the following:

>>> Range (1, 2 ) [1, 3, 5, 7, 9]

That, if you want to get even, you need to traverse each item, judge

>>> a = [] for in range (1, one ): ...      . if x% 2 = = 0:              ... >>> >>> a[2, 4, 6, 8, ten]

He is equivalent to the following derivation expression:

 for inch if x% 2 = = 0]>>> b[2, 4, 6, 8, ten]

A word.

Generate a coordinate system?

>>> dot = [(x, Y) forXinchRange (1, 10) forYinchRange (1, 10 ) ]>>>dot[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2), 7), (2, 8), (2, 9), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 1), (4, 2), (4, 3),  ), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 1), (5, 2), (5, 3), (5), (4, 5), (5, 5), (6, 5), (7, 5), (8, 5), (9, 6), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7), (  7, 8), (7, 9), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (9, 1), (9, 2), (9, 3), (9), 5), (9, 6), (9, 7), (9, 8), (9, 9)]>>>

Equivalent to the following 2 heavy loops:

>>> dot = []>>> forXinchRange (1, 10 ):...      forYinchRange (1, 10): ... dot.append ((x, y)) ...>>>dot[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2), 7), (2, 8), (2, 9), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (4, 1), (4, 2), (4, 3),  ), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (5, 1), (5, 2), (5, 3), (5), (4, 5), (5, 5), (6, 5), (7, 5), (8, 5), (9, 6), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (7), (1, 7), (2, 7), (3, 7), (4, 7), (5, 7), (6, 7), (  7, 8), (7, 9), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (9, 1), (9, 2), (9, 3), (9), 5), (9, 6), (9, 7), (9, 8), (9, 9)]>>>

Generate a list of incrementing strings:

>>> ['The number:%s'% n forNinchRange (1, 10 ) ]['The number:1','The number:2','The number:3','The number:4','The number:5','The number:6','The number:7','The number:8','The number:9']

Ask for 1-9 square of each number

 for  in range (1, ten ) [1, 4, 9, (+), +, +, +, Bayi]

Wrap a layer in a dictionary, and the same key will overwrite the previous

 for inch  for  in range (1, 5 )] {1:4, 2:4, 3:4, 4:4} for in range (1, 5 )]  [(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 1), (4), 3), (4, 4)]

List of references, with JavaScript type:

>>> L = ['my','name',' is','GHOSTWU' ]>>>l['my','name',' is','GHOSTWU']>>> A =L>>> A[3] ='Wukong'>>>l['my','name',' is','Wukong']>>>dela>>>l['my','name',' is','Wukong']>>>Atraceback (most recent): File"<stdin>", Line 1,inch<module>Nameerror:name'a'  is  notdefined>>>

Del A, a reference to the list is deleted, similar to the PHP garbage collection mechanism, two variables point to a list, delete one, but the other points to the list.

>>> a = [10, 20, 30 ]>>> B =a>>>b[10, 20, 30]>>>dela>>>Atraceback (most recent): File"<stdin>", Line 1,inch<module>Nameerror:name'a'  is  notdefined>>>b[10, 20, 30]>>>

Del a[], this refers to emptying the list

>>> a = [All, ten, ]>>> b = adel  a[:]>>> >>>  A[]>>> b[]

Powerful and elegant list derivation expressions in Python

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.