`
Suninny
  • 浏览: 38423 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
最近访客 更多访客>>
社区版块
存档分类
最新评论

Functional and List Comprehensions and Iterator

阅读更多

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)及其条件筛选法

    014Python中的列表推导式(List Comprehensions)及其条件筛选法

    列表推导(list comprehensions) 场景1:将一个三维列表中所有一维数据为a的元素合并,组成新的二维列表

    在Python编程语言中,列表推导(List Comprehensions)是一种高效、简洁的创建列表的方法。这个特性使得处理集合数据时代码更为优雅且易于理解。本篇内容将深入讲解列表推导的概念,以及如何利用它来解决实际问题,...

    Complex Network Analysis in Python

    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.

    The Functional Approach to Data Management.

    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的练习唱和备忘清单

    学习Python的练习唱和备忘清单 Getting Started What is Python Python Syntax Variables Operators Arithmetic Operators (+, -, *...Lists and their methods (including list comprehensions) Tuples Sets and their

    list的用法的源代码资源

    7. **列表解析(List Comprehensions)** - 一种简洁的创建新列表的方式。 ```python squares = [x**2 for x in range(10)] ``` 8. **列表推导式与生成器表达式** - 当需要节省内存时,可以使用生成器表达式。...

    Python Cookbook, 2nd Edition

    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 ...

    Oreilly Python Cookbook 2Nd Edition

    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库 | flake8_comprehensions-3.1.4-py3-none-any.whl

    资源分类:Python库 所属语言:Python 资源全名:flake8_comprehensions-3.1.4-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    python的list用法.doc

    此外,列表推导式(List Comprehensions)是一种简洁且高效的方式来创建新的list。 在实际编程中,理解并熟练运用这些list操作是至关重要的,它们能够帮助你更有效地处理和操作数据。在Python中,list被广泛应用于...

    Advanced Python for Biologists

    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` 还支持列表推导式(List Comprehensions),这是一种简洁的创建新列表的方式,例如: ```python squares = [x**2 for x in range(10)] ``` 这将创建一个包含 0 到 9 的平方的列表。 在处理大量数据时...

    Python Tutorial 入门指南3.6英文版

    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 ...

Global site tag (gtag.js) - Google Analytics