Python学习之路18——控制语句,迭代器, 列表解析

条件语句

if
都是大家的老朋友了,直接代码示例,简单易上手:
<span style="font-size:18px;">>>> lang = "python"
>>> if lang == "C":
...     print "C language"
... elif lang == "Java":
...     print "Java language"
... else:
...     print "Python language"
... 
Python language
>>> </span><span style="font-size:14px;">
</span>

循环语句

while
直接上代码,就是干
<span style="font-size:18px;">>>> count = 0
>>> while(count < 9):
...     print 'the index is:', count
...     count += 1
... 
the index is: 0
the index is: 1
the index is: 2
the index is: 3
the index is: 4
the index is: 5
the index is: 6
the index is: 7
the index is: 8
>>> </span><span style="font-size:14px;">
</span>

for
不解释,直接上
<span style="font-size:18px;">>>> game = ['LOL', 'GT', 'CSOL']
>>> for g in game:
...     print 'you play %s' % game
... 
you play ['LOL', 'GT', 'CSOL']
you play ['LOL', 'GT', 'CSOL']
you play ['LOL', 'GT', 'CSOL']

>>> for index in range(len(game)):
...     print 'you play %s' % game[index]
... 
you play LOL
you play GT
you play CSOL
>>> </span><span style="font-size:14px;">
</span>

break, continue, pass
break: 熟悉C的朋友都熟悉,打破循环
continue: 跳过后面的语句直接进行下一次循环
pass:空语句, 就比如C中空的大括号

while-else, for-else(还是挺方便的)
当循环和else语句一起使用时(描述不是很准确,看代码就懂了),循环若正常退出,则会运行else中的语句。如何循环是通过break语句结束的,那么else中的语句也将跳过。
下面引用书上的代码:
maxFact.py
<span style="font-size:18px;">def showMaxFactor(num):
        count = num/2
        while count > 1:
                if num % count == 0:
                        print 'largest factor of %d is %d' % (num, count)
                        break;
                count -= 1
        else:
                print num, 'is prime'

for eachNum in range(10,21):
        showMaxFactor(eachNum)</span><span style="font-size:14px;">
</span>
运行结果:
<span style="font-size:18px;">linux-ne7w:~/python> python maxFact.py 
largest factor of 10 is 5
11 is prime
largest factor of 12 is 6
13 is prime
largest factor of 14 is 7
largest factor of 15 is 5
largest factor of 16 is 8
17 is prime
largest factor of 18 is 9
19 is prime
largest factor of 20 is 10</span><span style="font-size:14px;">
</span>

迭代器

迭代器为类序列对象提供了一个类序列的接口。迭代器非常灵活,它可以迭代那些表现出序列行为的对象。例如字典的键,一个文件的所有行等等。
  • 为什么要迭代器:
  • 提供了可扩展的迭代器接口
  • 对列表迭代提供了性能上的增强
  • 在字典迭代中性能提升
  • 创建真正的迭代接口,而不是原来的随机对象访问
  • 与所有已经存在的用户定义的类以及扩展的模拟序列和映射的对象向后兼容
  • 迭代非序列集合时,可以创建更简洁可读的代码
怎么迭代:
首先可以用iter()函数返回被迭代对象的迭代器,然后调用迭代器的next()方法获取下一项。当所有项都获取了,会引发一个StopIteration异常,告诉调用者迭代完成。迭代器是单向通行的,只能向后移动。
使用迭代器:
<span style="font-size:18px;">>>> t = (123, 'text', 66.66)
>>> t
(123, 'text', 66.66)
>>> i = iter(t)
>>> i.next()
123
>>> i.next()
'text'
>>> i.next()
66.66
>>> i.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
>>> </span><span style="font-size:14px;">
</span>
若在实际应用中,这样写实在够呛。
python实在太智能,在实际的应用中,python会帮你处理。看一个迭代文件的例子
<span style="font-size:18px;">>>> f = open('text.txt', 'r')
>>> for eachLine in f:
...      print eachLine,
... 
This is just for a test

everyone should learn programming, programming teach you thinking.

forget my poor english.haha.</span><span style="font-size:14px;">
</span>
for循环会自动调用迭代器的next()方法,并处理StopIteration异常。
警告:在迭代可变对象的时候不要修改它,,到时候就boom,程序崩溃了。

列表解析

列表解析(List comprehensions)来自于函数式编程语言Haskell。它用来动态的创建列表。列表解析的语法:
[expr for iter_var in iterable]
按我的理解(生成列表元素),它等价于:
for iter_var in iterable:
expr

看几个例子:
<span style="font-size:18px;">[x ** 2 for x in range(6)]
[0, 1, 4, 9, 16, 25]
>>> seq = [11, 10, 89, 32, 43, 22, 55, 23, 21, 32]
>>> [x for x in seq if x % 2]
[11, 89, 43, 55, 23, 21]</span>
<span style="font-size:18px;">>>> [(x+1,y+1) for x in range(3) for y in range(4)]
[(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4)]
>>> </span><span style="font-size:14px;">
</span>

生成器表达式

生成器表达式是列表解析的一个扩展。目前还不是很熟悉,就先这样吧。





评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值