1. 将数列 a, b, c 对应的值相加
>>> a = range(0, 10)
>>> b = range(10,20)
>>> c = range(20,30)
#有时候像filter/map/reduce这样的"老资格"配合lambda用起来还是挺舒服滴
>>> map(lambda x,y,z: x+y+z, a,b,c)
[30, 33, 36, 39, 42, 45, 48, 51, 54, 57]
#此处用List Comprehensions 就没那么方便啦
>>> [a[i]+b[i]+c[i] for i in range(len(a))]
[30, 33, 36, 39, 42, 45, 48, 51, 54, 57]
2. 将数表中值相加取和(ps. Python 本身就内置了这样的 sum 函数):
>>> mysum = lambda l: reduce(lambda x,y: x+y, l)
>>> mysum(range(100)
4950
3. 矩阵变换
>>> mat = [
... [1, 2, 3],
... [4, 5, 6],
... [7, 8, 9],
... ]
# List Comprehensions 最直接
>>> [[row[i] for row in mat] for i in [0, 1, 2]]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
# Functional 也还行
>>> map(lambda i: map(lambda row: row[i], mat), [0, 1, 2])
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
# 也可以用内置的zip函数,不过返回的是list of tuple(这种构造便于作迭代)
>>> zip(*mat)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
#例1用外部迭代器比较好,像Ruby这种采用内部迭代器的语言表达起来不方便
#要么就学习LP采用“原始”的方法:
out = []
a.each_with_index { |x, i| out << x+b[i]+c[i] }
#例2Ruby可以用open class和inject来完成
class Array
def sum
inject(nil) { |s, x| s ? s+x : x }
end
end
>>> (1..99).to_a.sum
4950
#例3,Ruby却能完全胜任
>>> (0..2).map { |i| mat.map { |row| row[i] } }
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
分享到:
相关推荐
014Python中的列表推导式(List Comprehensions)及其条件筛选法
在Python编程语言中,列表推导(List Comprehensions)是一种高效、简洁的创建列表的方法。这个特性使得处理集合数据时代码更为优雅且易于理解。本篇内容将深入讲解列表推导的概念,以及如何利用它来解决实际问题,...
This book is an excellent read for anyone who wants to learn the fundamentals of complex network ...woods of data frames and list comprehensions and use your CNA intuition to grasp programming concepts.
Advances in Analysis and Optimisation using a Functional Appmach Section Editors 's Preface by Alexandra Poulovassilis 11. Analysis of Functional Active Databases James Bailey and Alexandra ...
学习Python的练习唱和备忘清单 Getting Started What is Python Python Syntax Variables Operators Arithmetic Operators (+, -, *...Lists and their methods (including list comprehensions) Tuples Sets and their
7. **列表解析(List Comprehensions)** - 一种简洁的创建新列表的方式。 ```python squares = [x**2 for x in range(10)] ``` 8. **列表推导式与生成器表达式** - 当需要节省内存时,可以使用生成器表达式。...
Updated for Python 2.4, it now includes over 200 recipes that range from simple tasks, such as working with dictionaries and list comprehensions, to complex tasks, such as monitoring a network and ...
Updated for Python 2.4, it now includes over 200 recipes that range from simple tasks, such as working with dictionaries and list comprehensions, to complex tasks, such as monitoring a network and ...
资源分类:Python库 所属语言:Python 资源全名:flake8_comprehensions-3.1.4-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
此外,列表推导式(List Comprehensions)是一种简洁且高效的方式来创建新的list。 在实际编程中,理解并熟练运用这些list操作是至关重要的,它们能够帮助你更有效地处理和操作数据。在Python中,list被广泛应用于...
How to transform data using Python’s comprehensions – How to write flexible functions and applications using functional programming – How to use Python’s iteration framework to extend your own ...
此外,`list` 还支持列表推导式(List Comprehensions),这是一种简洁的创建新列表的方式,例如: ```python squares = [x**2 for x in range(10)] ``` 这将创建一个包含 0 到 9 的平方的列表。 在处理大量数据时...
5.1.4. Nested List Comprehensions 46 5.2. The del statement 47 5.3. Tuples and Sequences 48 5.4. Sets 50 5.5. Dictionaries 51 5.6. Looping Techniques 53 5.7. More on Conditions 55 5.8. Comparing ...