python 语义理解
Python列表终极指南! (Python List Ultimate Guide!)
List Comprehension is one of the most powerful features available in dealing with Python lists. This feature is already at your fingertips. No need to import any library or modules, since list comprehension is a built-in Python feature.
列表理解是处理Python列表时可用的最强大的功能之一。 此功能已唾手可得。 列表导入是内置的Python功能,因此无需导入任何库或模块。
如何编写列表理解 (How to Write List Comprehension)
List Comprehension can be used to create lists with restrictions (written in mathematical statements) in just one line. Besides that, list comprehension can be used to make a new list from another list.
List Comprehension可用于仅在一行中创建具有限制(以数学语句编写)的列表。 除此之外,列表理解可以用来从另一个列表中创建一个新列表。
List comprehension syntax is easy to understand, don’t worry it is straightforward. To do a List Comprehension, you need to first make a square bracket []
. Inside the bracket, you will need to use the for keyword followed by one or more clauses of your choice. The expressions here can be anything and you can pour your creativity.
列表理解语法很容易理解,不用担心它很简单。 要进行列表理解,您需要首先制作一个方括号[]
。 在括号内,您将需要使用for关键字,然后使用您选择的一个或多个子句。 这里的表达可以是任何形式,您可以倾注您的创造力。
基本语法 (The Basic Syntax)
[expression for element in list if conditional]
The above List Comprehension is equivalent to:
上面的列表理解等效于:
for element in list:
if conditional:expression #appends the resulting element to the list
例子 (Examples)
Of course, I will provide you with some examples! This article won’t be complete otherwise. I will start with the simpler one and continue to the more advanced applications.
当然,我会为您提供一些示例! 否则,本文将不完整。 我将从较简单的应用程序开始,然后继续至更高级的应用程序。
产生新清单 (Generating a New List)
1. Making a new list that consists of the first 5 natural numbers.
1.制作一个由前5个自然数组成的新列表。
ls = [i for i in range(1, 6)] #ls = [1, 2, 3, 4, 5]
2. Making a new list of the first 5 even natural numbers.
2.重新列出前5个偶数。
ls = [i for i in range(2, 11, 2)] #ls = [2, 4, 6, 8, 10]
or by using if for the sake of showing how to use conditional in making a new list.
或通过使用if来显示如何在创建新列表时使用条件。
ls = [i for i in range(2, 11) if i % 2 == 0] #ls = [2, 4, 6, 8, 10]
3. Making a list of the first 5 square numbers
3.列出前5个平方数
ls = [i**2 for i in range(1, 6)] #ls = [1, 4, 9, 16, 25]
使用我们声明的功能 (Using a Function We Declared)
The conditional for list comprehension does not need to be written explicitly inside the square bracket. You can declare a function beforehand and just call it inside the list comprehension. In the next example, we will be doing this.
列表理解的条件不需要明确地写在方括号内。 您可以预先声明一个函数,然后在列表推导中调用它。 在下一个示例中,我们将这样做。
4. Making a list of the prime numbers between 1 to 30 inclusive
4.列出1至30之间的质数
import mathdef isPrime(n):
if n == 1:
return False
elif n == 2:
return True
elif n % 2 == 0:
return False
else:
for i in range(3, int(math.sqrt(n)) + 1, 2):
if n % i == 0:
return False
return Truels = [i for i in range(1, 31) if isPrime(i)]
#ls = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
从另一个清单制作新清单 (Making a New List from Another)
Another useful usage of list comprehension is making a new list from another. You can think about it this way, you are selecting the elements from the original list that fulfill your requirements. Then, you group these items to a new list. Here are some examples:
列表理解的另一个有用用法是从另一个列表创建新列表。 您可以通过这种方式进行思考,即从原始列表中选择满足您要求的元素。 然后,您将这些项目分组到一个新列表。 这里有些例子:
5. Taking only the even numbers from the original list
5.仅取原始列表中的偶数
ls = [15, 6, 1, 3, 10]result = [i for i in ls if i % 2 == 0] #result = [6, 10]
6. Finding the elements in a list that also exist in another
6.在列表中查找在另一个列表中也存在的元素
ls = [15, 6, 1, 3, 10]
ls2 = [1, 2, 3, 4, 5, 6]result = [i for i in ls if i in ls2] #result = [6, 1, 3]
最后的话 (Final Words)
Python List Comprehension is a technique that you definitely want in your Programming toolbox. It is powerful and easy to be implemented anywhere you want. After some time, trust me, you will be used to it. After this point, you can easily use it anytime you want to. If you find this article useful and want to know more about other Python tricks, you can read this article: Become a Python “One-Liners” Specialist.
Python List Comprehension是您肯定需要在“编程”工具箱中使用的一项技术。 它功能强大且易于在您想要的任何地方实施。 一段时间后,请相信我,您会习惯的。 此后,您可以随时随地轻松使用它。 如果您发现本文很有用,并且想进一步了解其他Python技巧,则可以阅读本文: 成为Python“一线”专家 。
Best Regards,
最好的祝福,
Radian Krisno
拉迪安·克里斯诺(Radian Krisno)
翻译自: https://towardsdatascience.com/python-list-comprehension-711330271401
python 语义理解