Python List Comprehensions
来源:互联网 发布:手机网络浏览器 编辑:程序博客网 时间:2024/06/06 03:17
An Introduction to List Comprehensions in Python
Tweet
Further reading.
- Set Builder Notation
- PEP 202 — List Comprehensions
- Python Patterns – An Optimization Anecdote
- Python Performance Tips / Loops
0 0
- Python List Comprehensions
- Python练习-- List Comprehensions
- Python Tutorial :Loops and List Comprehensions 其他
- Python 列表生成式(List Comprehensions)
- Python基础-列表生成式-List Comprehensions
- Python 列表生成式(List Comprehensions)
- Python高级特性之:List Comprehensions、Generator、Dictionary and set comprehensions
- Python高级特性之:List Comprehensions、Generator、Dictionary and set comprehensions
- 【快乐Python之一】List Comprehensions 和 Generator Expressions
- Python之列表推导式List comprehensions例解
- The study of List Comprehensions in Python(20170911)
- 转载:Python高级特性 列表生成式(List Comprehensions)
- Python教程学习简记5--Python 列表生成式(List Comprehensions)
- 列表推导式(list comprehensions)
- (完)learn python第五章:更多类 (第六节:递推式构造列表list comprehensions)
- 5.1.4. List Comprehensions(列表解析)
- List Comprehensions (列表推导式)
- [Python] 字典推导 PEP 274 -- Dict Comprehensions
- Leetcode: Find the Duplicate Number
- 非接触开关
- Scrollviewer 无法滚动 的原因
- Android 性能优化学习笔记
- 多线程的异常处理
- Python List Comprehensions
- 解决:Some projects cannot be imported because they already exist in the workspace
- 【C#基础】运算符二
- python的github3模块详解
- 一维数组(C# 编程指南)
- 导入android sample时遇到的问题以及解决方案
- PKU3461Oulipo
- STM32 定时器输入捕获注意点
- java replaceall 用法
The basics.
Let’s start with a simple list.
Quite often, we’ll need to build a new list from the elements of an existing list. For example, let’s say we want to make a list containing all elements of
my_list
, each multiplied by 2. A common way to do this is by iterating through the existing list and building the new list:Now let’s take a look at how to do this using Python’s list comprehension syntax.
Syntax.
The syntax for list comprehension is based on set builder notation. Given the form
[Y for X in LIST]
,Y
is commonly referred to as the output function ,X
is the variable, andLIST
is the input set. This statement says to doY
on eachX
inLIST
.For an example of using a different output function , let’s say say we want a new list that contains each number in
my_list
as a string type…You may be asking yourself why we wouldn’t just use
map()
here:In this example,
map
looks like a good alternative. However, for slightly more complicated requirements, list comprehension can be a bit more concise. For example, when we’d like to specify a predicate .The predicate.
Consider our previous example, where we want to create a list of string types for each element in
my_list
, except this time we only want the elements which are even numbers. A typical way to do this would be:In order to use
map()
here we would need to firstfilter
the list to exclude the odd numbers.Using the list comprehension syntax would look like this:
This version introduces the predicate, an expression after the list which acts as a filter on which elements get passed to the output function.
Neat, clear and concise.
Loops of loops.
Finally, it’s worth mentioning that you can use list comprehensions to iterate on more than one list. For example:
Just like you would expect in
for
loops, the last loop moves the fastest. Also note that this method returns a list oftuples
. If you’d like nested lists, you can also nest one list comprehension within another.Summary.
List comprehension in Python can often provide a neat, clear, and concise syntax for creating lists from other lists. However, one should always be aware that, particularly for complex transformations or predicates, the concise and terse syntax can quickly become very difficult to read. In these cases, it’s often beneficial to revert to traditional looping constructs.