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)及其条件筛选法
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 ...
This includes a class and module system, a syntax that is cleaner, less terse, and well structured, and list comprehensions that make working with collections of data more enjoyable. CoffeeScript can...
分享Python高级用法的个人总结 ppt文档,场景1:将一个三维列表中所有一维数据为a的元素合并,组成新的二维列表。
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 building a templating ...
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.
This includes a class and module system, a syntax that is cleaner, less terse, and well structured, and list comprehensions that make working with collections of data more enjoyable. CoffeeScript can...
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 ...
List Comprehensions Boolean Operations Processing Multiple Lists Sorting Adding Items To A List Removing Items From a List Reversing a List Removing Duplicate Values Searching for Values Counting
Like its predecessor, the new edition offers a collection of ...as working with dictionaries and list comprehensions, to complex tasks, such as monitoring a network and building a templating system.
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
It aids development by adding a bevy of features, such as classes, splats, list comprehensions, and destructuring. These features make it easier to write clear, readable code, and by learning how ...
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 ...
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 ...
Using List Comprehensions Receiving Tuples and Lists in Functions Lambda Forms Using Lambda Forms The exec and eval statements The assert statement The repr function Summary 16. What Next? ...