directory

  • An introduction to Python list derivations
  • 2.Python list derivation syntax
  • 3.Python list derivation exercises
    • 1.Python list derivation Case 1
    • 2.Python list derivation case 2
    • 3. Comparison of Python list derivations and loop efficiency
  • Four. Key summary
  • Five. Guess you like it

Recommended path for learning Python: Python Learning Directory >> Python Basics

An introduction to Python list derivations

In the last article, we introduced Python conditional derivations, which are essentially the operation of combining multiple lines of code into one line. Using list derivations makes programs run more efficiently (explained at the end of this article). List derivations are similar to conditional derivations;

Python list comprehensions are a way to quickly and succinctly create data types from one or more iterators. They combine loops with conditional judgments to avoid syntactically lengthy code and improve code efficiency. Being able to use derivations is also an indirect indication that you are beyond the beginner level of Python. 支那

Python derivations are related:

  • Conditional derivation
  • List derivation
  • Dictionary derivation

2.Python list derivation syntax

List comprehensions are conditional comprehensions used in conjunction with loops and return oneList the list, and the entire expression needs to be inside [], because the return value is also the list list.

In the for loop, if the value of x satisfies the condition, it returns exp1. Data: a sequence (e.g., list/tuple/string) condition: "[exp1 for x in data if condition]" In the for loop, if the value of x satisfies the condition expression, return exp1; Exp2: In the for loop, if the value of x satisfies the condition, it returns exp1; [exp1 if condition else exp2 for x in data] [exp1 if condition else exp2 in data]Copy the code

3.Python list derivation exercises

1.Python list derivation Case 1

Get all even numbers from 0 to 20 and multiply by 10, and return all computed results. Example code is as follows :(implemented using Python list derivation syntax 1)

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python list of pythons. py @time :2021/3/27 08:00 @motto: A thousand miles without a small step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! 1. Since 0~20 is obtained, including 20, Range (0,21) 2.x*10 exp1 3.x%2 == 0 condition 4.range(0,21) data (sequence) "" list1 = [x*10 For x in range(0,21) if x%2 == 0] print(list1) print(type(list1))"  [0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200] <class 'list'> '''Copy the code

2.Python list derivation case 2

Multiply an even number from 0 to 20 by 10 and an odd number by 100, and return all computed values. Example code is as follows :(implemented using Python list derivation syntax 2)

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python list of pythons. py @time :2021/3/27 08:00 @motto: A thousand miles without a small step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! 1. Since 0~20 is obtained, including 20, Range (0,21) 2. X *10 exp1 3. X *100 exp2 4. X %2 == 0 List2 = [x*10 if x%2 == 0 else x*100 for x in range(0,21)] print(list2) print(type(list2)) [0, 100, 20, 300, 40, 500, 60, 700, 80, 900, 100, 1100, 120, 1300, 140, 1500, 160, 1700, 180, 1900, 200] <class 'list'> '''Copy the code

3. Comparison of Python list derivations and loop efficiency

For those of you who are wondering, why do I use this stupid derivation when I can do something with a for loop or a while loop?

Using a list derivation is much more efficient than a for loop. You might print(” HelloWorld “) in 0.0002 seconds for the CPU, and you might not feel the difference. What if you needed to print helloWorld 100 million times? Often details feel success or failure!

If you have a requirement to store all integers up to 0 to 10 million (100 million) in a list, compare the Python list derivation with the for loop time:

#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python list of pythons. py @time :2021/3/27 08:00 @motto: A thousand miles without a small step, a river without a small stream, the wonderful life of the program needs to be accumulated with perseverance! "" import time # add time module, Total_num = 10000000 # start_time = time.time() list1 = [x for x in Range (0,total_num) print(" range(0,total_num) print(" Format (end_time-start_time)) # Use the ordinary for loop start_time = time.time() list2 = list() for x in range(0,total_num): Append (x) end_time = time.time() print(" format(end_time_start_time) ") 0.5455152988433838 seconds For loop time: 1.2068836688995361 secondsCopy the code

The output is clear, and the Python list derivation is fully twice as efficient as a regular for loop, with universal functionality.

Finally warm reminder: test please change the number of cycles a little bit smaller, after all, just began to add a few laps carelessly, the computer crashed!

Four. Key summary

  • 1. Note that when Python list comprehensions are written, all expressions must be inside [] because the return value is the list list
  • 2. List derivation is a Python development learning knowledge, practice makes perfect.

Five.Guess you like

  1. Python Configuration Environment
  2. Python variable
  3. Python operator.
  4. Python conditions determine if/else
  5. Python while loop
  6. Python break
  7. Python continue
  8. The Python for loop
  9. The Python string
  10. The Python list
  11. The Python tuple tuple
  12. Python dictionary dict
  13. Python conditional derivations
  14. Python list derivations
  15. Python dictionary derivations

Python list derivations

This article is published by the blog – Ape Say Programming Ape Say programming!