Skip to Content
Python语言及其应用(第2版)
book

Python语言及其应用(第2版)

by Bill Lubanovic
March 2022
Intermediate to advanced
522 pages
13h 52m
Chinese
Posts & Telecom Press
Content preview from Python语言及其应用(第2版)
模块、包和赠品
181
>>> palindrome('radar')
True
>>> palindrome('halibut')
False
上述例子是双端队列的简单演示。如果你想快速检查回文,那么更简单的做法是将字符串
与其逆向形式进行比较。
Python
并未提供字符串的
reverse()
函数,不过可以使用切片获
得同样的效果。
>>> def another_palindrome(word):
... return word == word[::-1]
...
>>> another_palindrome('radar')
True
>>> another_palindrome('halibut')
False
11.3.5
使用
itertools
迭代代码结构
itertools
包含特殊用途的迭代器函数。在
for...in
循环中调用时,迭代器函数每次会返
回一项,并记住当前的调用状态。
chain()
会遍历其参数,将其视为单个可迭代对象:
>>> import itertools
>>> for item in itertools.chain([1, 2], ['a', 'b']):
... print(item)
...
1
2
a
b
cycle()
是一个无穷迭代器,会循环遍历其参数:
>>> import itertools
>>> for item in itertools.cycle([1, 2]):
... print(item)
...
1
2
1
2
.
.
.
accumulate() ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano
Python实用技能学习指南

Python实用技能学习指南

Posts & Telecom Press, Robert Smallshire, Austin Bingham
Python技术基础视频教程

Python技术基础视频教程

保罗·J·戴特尔
Python面向对象编程指南

Python面向对象编程指南

Posts & Telecom Press, Steven F. Lott

Publisher Resources

ISBN: 9787115586223