Python's Recursive construction list (listing comprehension)

Source: Internet
Author: User
Tags square root

Introduced

We learned "LAMBDA operations, filter, reduce and Map" in the previous chapter, but compared to map, filter, reduce and LAMDBA, Guido van Rossum prefers to use a recursive construction list Comprehensio N). In this chapter we will cover the basic functionality of the recursive construct list (comprehension). The recursive construct list (comprehension) is added in Python 2.0. Essentially, it is a form of python used by mathematicians to implement well-known markup collections.

Mathematically, the number of squares of natural numbers is:{x2 | x∈} or plural:{(x, y) | x∈?∧y∈?}.

In Python, recursive construction lists (list comprehension) are an elegant way to define and create lists, which are often collections of constraints, not a collection of all cases.

for function map (), filter (), and reduce (), the recursive construct list (comprehension) is a complete lambda replacement. For most people, the syntax for recursive construction lists (list comprehension) is easier for people to grasp.

Lifting columns

In the LAMDBA and map () chapter, we have designed the map () function to convert the value of Celsius to the value of Fahrenheit and its inverse function. By using a recursive construct list comprehension, you can say:

>>> Celsius = [39.2, 36.5, 37.3, 37.8] for in Celsius]print  fahrenheit[102.56, 97.700000000000003, 99.140000000000001, 100.03999999999999]

The following recursive construct lists (list comprehension) Create the Pythagorean ternary group:

 for inch  for inch  for inch if x**2 + y**2 = = Z**2[(3, 4, 5), (5, 12, 13), (6, 8, 10), (7, 24, 25), (8, 15, 17), (9, 12, 15), (10), (+, +), (+, +), (+, +),

Two cross-product of a set:

>>> colours = ["Red","Green","Yellow","Blue" ]>>> things = [" House","Car","Tree" ]>>> coloured_things = [(x, Y) forXinchColours forYinchthings]>>>Printcoloured_things[('Red',' House'), ('Red','Car'), ('Red','Tree'), ('Green',' House'), ('Green','Car'), ('Green','Tree'), ('Yellow',' House'), ('Yellow','Car'), ('Yellow','Tree'), ('Blue',' House'), ('Blue','Car'), ('Blue','Tree')]>>>
Recursive construction Generator (Generator comprehension)

The Recursive construction generator (generator comprehension) was introduced in Python2.6. They are a simple build expression enclosed in parentheses, except that its syntax and work are much like a recursive construct list (lists comprehension), but a recursive construction generator (generator Comprehension) returns a generator instead of a list.

 for  in range)print0xb7307aa4>>>> x = list (x)Print 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361] 
Some of the more advanced examples

Use the Eratosthenes sieve method (Sieve of Eratosthenes) to calculate prime numbers from 1 to 100:

 for inch  for  in range (i*2, +, i)]for in ifnot in Noprimes]  print  primes[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 7 9, the.

We write the previous example in a more popular format, so we can calculate the list of prime numbers of any number n:

 from Import sqrt>>> n = 100>>> sqrt_n = int (sqrt (n)) for in range (i*2, n, i)]

If we go to see the content of No_primes, we will find a problem. Here are a number of repeating elements in this list:

>>> no_primes[4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 5 0, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 6, 9, 12, 15, 18, 21, 2 4, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55 , 75, 80, 85, 90, 95, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78, 84, 90, 96, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, -------------------------98, +,-----------------

This intolerable problem will be resolved in the recursive construction set (set comprehension), which we will explain in the next section

Recursive construction Set (set comprehension)

The recursive construction set (set comprehension) is very similar to a recursive construction list (lists comprehension), but returns a collection instead of a list. Syntactically, we will use curly braces instead of square brackets to create a collection. The recursive construction set (set comprehension) is the correct way to resolve the problem in the previous section. We can create a set of non-mass numbers that do not have duplicate elements:

>>> fromMathImportsqrt>>> n = 100>>> sqrt_n =Int (sqrt (n))>>> no_primes = {J forIinchRange (2,sqrt_n) forJinchRange (i*2, N, i)}>>>no_primes{4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, 28, 30, 32, 33, 34, 35, 36, 38, 39, 40, 42, 44, 45, 46, 48 , 49, 50, 51, 52, 54, 55, 56, 57, 58, 60, 62, 63, 64, 65, 66, 68, 69, 70, 72, 74, 75, 76, 77, 78, 80, 81, 82, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 98, 99}>>> primes = {i forIinchRange (N)ifI not inchNo_primes}>>>Print(primes) {0,1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}>>>
Using recursive functions to calculate prime numbers The following Python script uses a recursive function to create prime numbers, and his function is to examine multiple primes of a square root to N:
 fromMathImportsqrtdefprimes (n):ifn = =0:return []    elifn = = 1:        return[1]    Else: P=primes (int (sqrt (n) )) no_p= {J forIinchP forJinchRange (i*2, N, i)} P= {X forXinchRange (2, N)ifX not inchNo_p}returnPPrint(Primes (40))
Reprint please indicate the source

Original (English): http://www.python-course.eu/list_comprehension.php

(Chinese): http://www.cnblogs.com/reanote/p/python_list_comprehension.html

Python's Recursive construction list (listing comprehension)

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.