您的位置:首页 > 编程语言 > Python开发

Python Tutorial :Loops and List Comprehensions 其他

2015-04-23 00:00 375 查看

目录

这是麻省理工大学(MIT)官方编程教程中Python Tutorial的内容,教材为《Think Python: How to Think Like a Computer Scientist》。这是我的学习笔记,因为水品有限,请大家多多包涵。如果有一起学习的同学可以一起交流。如笔记中错误,请一定要告诉我啊,我肯定及时改正。所有笔记的目录详见:MIT:Python Tutorial目录

Python Tutorial :Loops and List Comprehensions

6.01 Python Notes, Section 4: Lists (PDF)

A list is written using square brackets(就是[]), with entries separated by commas. You can get elements out by specifying the index of the element you want in square brackets, but note that the indexing starts with 0!

(list使用[]来定位index,且,从0开始计算)

First, here is something like you might have learned to write in a Java class (actually, you would have used for, but Python doesn’t have a for that works like the one in C and Java).

(python中的for语句的功能与java和C中的功能不一样,所以请不要搞错)

def addList2(l):
sum=0
for i in range(len(l)):
sum=sum+l[i]
return sum


Here’s a version using Python’s for loop. The range function returns a list of integers going from 0 to up to, but not including, its argument. So range(3) returns (0, 1, 2). A loop of the form

这是使用了Python中for loop的语句.这是因为rang()这个函数返回一个从0到最大值的整数的list,而for只能用于list中.如果使用下面这种方式,则可能错误:

for i in l:
sum=sum+l[i]


will be executed once for each element in the list l, with the variable x containing each successive element in l on each iteration.

应该写成:

def addList3(l):
sum=0
for v in l:
sum=sum+v
return sum


当然也有更懒的方法,就是直接调用sum函数:

def addList4(l):
return sum(l)


6.01 Python Notes, Section 5: Functional Style (PDF)

这部分讲函数的叠加.和将函数当参数处理

5.1 Basics

But let’s begin at the beginning. The primitive elements in the functional programming style are basic functions. They’re combined via function composition: so, for instance f(x, g(x, 2)) is a composition of functions. To abstract away from the details of an implementation, we can define a function.

函数的定义:使用lambda

We can construct functions “anonymously(不具名的)” using the lambda constructor:

>>> f=lambda x:x*x
>>> f
<function <lambda> at 0x02DBD2B8>
>>> f(4)
16


The word lambda followed by a sequence of variables separated by commas, then a

colon, then a single expression using those variables, defines a function. It doesn’t need to have an explicit return ; the value of the expression is always returned. A single expression can only be one line of Python, such as you could put on the right hand side of an assignment statement. Here are some other examples of functions defined using lambda. (lambda自带return功能)

>>> g=lambda x,y:x*y
>>> g(3,4)
12


You don’t need to name a function to use it. Here we construct a function and apply it all in one line: (你甚至不需要给函数命名,并可以在一个行上使用它)

>>> (lambda x,y:x*y)(3,4)
12


5.2 List Comprehensions

Python has a very nice built-in facility for doing many interative operations called list comprehensions. The general template is

[expr for var in list]

where var is a single variable, list is an expression that results in a list, and expr is an expression that uses the variable var. The result is a list, of the same length as list, which is obtained by successively letting var take values from list, and then evaluating expr, and collecting the results into a list.

Whew. It’s probably easier to understand it by example.

>>> [x/2.0 for x in [4,5,6]]
[2.0, 2.5, 3.0]


>>> nums=[1,2,3,5,6,88,99,101,10000,100,37,101]
>>> [x for x in nums if x%2==1]
[1, 3, 5, 99, 101, 37, 101]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 教程