• 知识点
  1. 迭代
  • 迭代

什么是迭代呢?和递归又有什么区别呢?

根据维基百科和网上的资料:

迭代:迭代是重复反馈过程的活动,其目的通常是为了接近并到达所需的目标或结果。每一次对过程的重复被称为一次“迭代”,

而每一次迭代得到的结果会被用来作为下一次迭代的初始值。

如求1+2+3+4+5的和:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAABTCAIAAADm0/dTAAAHPUlEQVR4nO2dv0sjTRiA/VtSZLc1YOGJAa/4viWJwtpogug1Cd4ViUTWQg40IghpEq2irb0gVgHxbxArt7juKw8CVrHKV2ySnZmd3cyMszNjfOEpzMZJ1t3Hd2Z2frwLlp0BAJNZ0H4GAJAMOAqYDjgKmA6Ho9/2jjvddqfhaD9py85YzuNdaxRwvbWh/3yA1GBzdKV80m2f7Dm7p2Y46jzetUbnTsayM9byzTVoOtewOOrUu8e7KxnLXjLD0Y3G4eiu6k2PbFdHd4c3a7ovJZASXO3RVBx1ey/D4duY/sHsIss319Mgamcs2ztvje5afxrL+q8mkAa6HT16eO0VJy8P+sM35GUMzmNoZODrFmEtMFfodhTH609D6WIv23yP0LcQR5dvrsMfwNG5Rb+jXv8trOtZqvvA0a1INHX0X00gDTQ76vXfhsMHD30509Hlm+vW6K71uD09gtb+wNyh19GDPh44mer6SL9+besP9OvnGL2OFnv+29C/cu2MNa30Wbr2keej45+BeYTJ0VKj3ekSBE9MP4x79Tppib72im7vhclRGxtnOnf0X0cgPWC8HjAdcBQwHXAUMB1wFDAdcBQwHXAUMB1wFDAdcBQwHVMcda0qPuCZ2hcF01UZRwrI08trv1CpcPSAzppI5KA/fOsfKT09VkfRoab6uuzzsMrv2WovTTWnBI7OnqVKlLKq79mmby2qvDeKcK9eiQuCjOFFR/KCC6hSU+ax0Okw/fov6Zp62eZ7tuRqv1tfEXJaj2UHjiLTyiK4vRfmuCsBgbo+hRl6c1yNmg3dtlmO0s1ODaMdRRup2C9Y5fdsybXzfa4m7NED38Kp8Lto52Av9rJN31pET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VxQQqXE1k4BVGC8HjAdmPcEmA7EUcB0II4CprOQX80pQ/tfK4u/f//Tfg5fB3BUBHBUJeCoCOCoSsBREcBRlXxiRzXmTAJHVcLpaKXe6bY73XrFGEf5ciYljVtyAI6qhMvR7/tnbXMcFQEc/YRwOFra/93p1iuVOjgKjqqE3VHX67Zb+9/zMh1NTHGUkISJP2cSbavbJuNGu9H1TOCoSlgdrXjtztmP0rhJKtNRxCR8Z+TZSZj4cyYJxFHaeiZwVCVsjhZ/tLptr5LLp+JoGDsxEWcnYeLPmfThuj6Y4wyOqoTF0e/7Z+2O545fyneUSLKIJgpLrov58ygIORpdzwSOqoTB0Uq90/29X8x9TUep65nAUZXMdrTiUZKKiD2Binw9qVSkrtfuKH09EziqEv5xpvTiqFXy+ZIwCeRMwnPjzoa+ngkcVYkJjsZsuxDv6IdyJlkln+vZE209EziqEr3j9dFA+DkAR1UCjooAjqoEHBUBHFXJJ56bpxFwVCWw5g4wHXAUMB1wFDAdcBQwnf8B69plgaAyH48AAAAASUVORK5CYII=" alt="" />

递归呢?指在函数的定义中使用函数自身的方法。也就是函数不停的调用自己直到满足一定条件。

而用递归是怎样求1--5的和呢?

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUcAAACECAIAAACMIH6SAAAPZUlEQVR4nO2de2xUVR7Hzx+7C/JQYXXXzSZbmJDeCSEkNOEPIYXgH7uT6UAyTQy0qbCJaUObpVML0yoCbbFYraxteXUG1I3WJUuMNaW6Om4ttEB5iFrrq1cBBeTvJv1r+gdh/7jzuI9z79zXzJx77zf5/OHM3NdUPvM759xzvpdEGv9x/ty5JUse5fw+AIALIE2RXbAaADcBqwFwG6Qpsuv8eVgNgHsgzzVFYDUAboI0PxcZGzvPtNXBvhp+prJe/y6RSn6mLsVQebEuu2U4mRyO6tq4KZGcTbQU++8M3AJpbn7OtNXrduzt6e3uiQTye5X1Q3X8ZCCob+NgXw0/U9MVLPJfNnxkOjk7HavQuX04NpWE2MAmyO7dpqxeX9X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vcaq2kLhO1unAhXvJ+dWeAdj1qy4alwGrvYN5qUQv8qZ0Hu3uiYfusztZnRWofNY5LtrvSavWkLhNWFy7ES4IQYR/fukXxkUrEhxRY7R2s1GrRO9WNPb37dlbYZbXYuqpsPJXmEmX67jlXFJvpVxcsxEvGlsjuB4O7T26Sv69r5TCs9g7mrZb0q71ltUABQrzkbKtVtRq1GmQwZ7W8yR3auc/ckLjiguQSqudmUymk1QJ5DfGSEe2kPUQK/Wogw8qdrUxxVpRuu6zmQrx4cCsfVqukgukmryFeErbVPqCOh2MMHMgwPwsltHNfZmKZ6btciguSBT4rH1ujFegnRZxrr5nURU8FU6WAIV7phzwK0EP29MZxwWrvwNo88Fwxum7GTIgX5pYBJbCaJYyGeBmJ44LV3gFWMwbWbAHLsGY1yBew2jsgjRAAtwGrAXAbsBoAtwGrAXAbsBoAtwGrAXAbsBoAtwGrAXAbsBoAtwGrAXAbsBoAt/F/5QxXrvMU+QkAAAAASUVORK5CYII=" alt="" />

斐波那契数列的递归和迭代实现:

 def fib(n):
if n>0:
if (n==1 or n==2):
return 1
else:
return fib(n-1)+fib(n-2)
else:
return -1
def fib_2(n):
if n<=0:
return -1
else:
a=1
b=1
s=1
for i in range(n-2):
s=a+b
a=b
b=s
return s
print fib(int(raw_input('Please input a number you want to calculate the fib number:')))
print fib_2(int(raw_input('Please input a number you want to calculate the fib number:')))
  • 那Python中哪些是可迭代的呢?

从左往右扫描对象的方式都是可迭代的

如何判断一个对象是不是可迭代的呢?

stackoverflow是这么说的。http://stackoverflow.com/questions/1952464/in-python-how-do-i-determine-if-an-object-is-iterable

  1. 使用Python的内置函数hasattr  ,hasattr(object,name)当object具有name属性时返回真。迭代属性就是__iter__.

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWsAAABRCAIAAACbshBQAAANVElEQVR4nO2dO08bSxSA729xwboFKQWJuIhu5QeSaTAIJZWJcGGjEKdIk1iyRJQigVSGFkpkCaWyZEFLibhNVoiWFonKqXyL3fXO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ASUVORK5CYII=" alt="" />

2.    使用collections模块的Iterable类型来判断

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYkAAAByCAIAAAApnbqEAAAUN0lEQVR4nO2dy28URxrA62+Zg2euIHFIIoOS3U1mZ8ZhZ4Bge40NcQzBB49FGB+ilYAIycgH7PjUyRUOe0BIyKeRkDlFm10pC/E+4l6WJYsfPGxwPGB2OLGHrumuqq6q7urpR5X9Sb8DeLqqq7urf1P1dfU3aOPpo1MnqvlCDgAAQB/QxtNHp07UMm8HAAAACdp4Bm4CAEA70OazlU+HwU0AAOgFev5sZXT4SObtAAAAIEHPN1ZHR7RzUzU/9qbnnEMzn317QjG10G63MM3JsB8ldAKtpdT2JWsGvo4Hs7865lG2bPEVTL1HpQ96sbE6evJohJLvjnw5OzczWy/G36z8wJueMcsUJbFIupS0t6lSnV9ut5pT/E8dNy1b5WzPhuMmO78vy2bkK7biN1yj2fLufP/9Lz3z8RGmt8TaozQDvdhY+0zVTe8NXJibuTBSHL6YiJsaPefe9FSqWZ+aqGjhJsBD3U3EGV6yqlmdeXDTpqqbihNzXw6/l8sXDiTpJnNnAeAmzQA3mQl6sbk2dupYpMLpuik/8KanUi0cbPLiUGR8yivrFMkPvHEGYvmK3XPuTc9AI2QzvIE91UFxKEc41Vd3kzR24G8G1QCPhUaI2gSNL1t2a9kqe5/a81VRC90dycGn/Rwbb5JdlH1Wzzk7v4+4mtTwmX+VxX2D3t4lbAfwuyngzEtO/tRC256vFiabnU9JtUkuSrs5KbwoUXuUKUQYN7nE7SaqN59je2fnU6Lb4U5G/juPO+hBoohzA4zZPQONsF+hk02qK5QtC1/gqrVE9MXJJufaK7ppakFcobAZ+ULgtzdnX+LGly2b7MeTTTJW1dUwYZ/F8YjoojgbuxedKiu8ytK+kS+kOG6SXMqOLJxS1IWYWiDCguSZ71wU3AFCdzZZjzIJ9GJjbXREDzd1EI+biB6Wr9g4Xn6wyfQ8N5TuFslXbByR9W3Mg76HSSabTKecWmiz3VfJ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kxShS5eg2Ok579m0s92pNLj3NNn/774+13JQ5xxrhO1+dySqI9XryoHq+MUk71EuzfkW79bUfHvwG8BnAA6Q4g3YG+uyC9NgPS64N0B+59Lzf606sRSw/0px8N/ekO0J/uAP3pDtCf7gD96Q4M3ZX+9NrE0sWaoBQKvcRjXGDorpN3unbrgXQHZulXpFfjDz6bFgqnboiaAAAAAElFTkSuQmCC" alt="" />

  • 对于一般的可迭代类型用for in就可以对它们进行迭代操作
  • 但是对于有键值对的字典呢?对于值value则需要itervalues()函数来对值进行操作
 capitals={'China':'beijing','America':'Washington','Russia':'Moscow'}
print 'countries:'
for country in capitals:
print country
print 'capitals:'
for capital in capitals.itervalues():
print capital

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAN8AAACACAIAAAAEQytZAAAEvUlEQVR4nO3dQXbTMBCAYR2SA7DgaG3P0W7TA8CeRbmFWaTNc62Z0ViS7bH1f49FojhKgB+bWMGk97fn+49/f3//+vkjAXGcpc5pmqZpOvpdYF+H1FnRGXWOaP86D+xs+nLIq2O1vM5pZr6lOD6/m9/Wts8fmt81nlJ8P8Z4os7TWdRp19Z+O7+bjxvNiXedr4vzMeqc263OSdp9apPMFefH+USrU3ufaxMUN8hTRmgHHtlXBWds7HzdRJ2n0/ipKH3/LRfL81SiTb54KJ/W/z5xPmc5G48RUSfiok7ERZ2IizoRF3UiLupEXGKdO5ws5Hwkyvxn4/uiTpQ5VzKBA6xaZxd3q/ZgUpbI/SuQrEyOa+23QCb3evpiG7E58a4WIoEOR6wz313ZdYq7t/nIqjoXtPlxfR33nQt2TM46PftUXNYOR3aRsbHnNoZQ8e1j8SFxA7GnKWM/ZT5IoGNhrQhxUSfiok7ERZ2IizoRF3UiLupEXMb3O8WTkdo8nIlEf9r3O++P+pdqqBP92ddRYiERR3Je5SuVrn9kDGork7SOgrV1auvdFTtdAkVBxb4ztdVp7FaBb+r+3lldp2efCnyq+8xOndhD3flO8aH5NkaF2mbAEmtFiIs6ERd1Ii7qRFzUibioE3FRJ+JyXqmmO850osy/VtQXdaLsqDqBMmednpXJSVp5bx/HuOwrc4tFttx+MCokUHwy9p3O2rSY7MjsfSd1IqUeR3Zj0D9enB8jaqyz15GdOiHQznfeH81va7HaR2pt3DM/xsVaEeKiTsRFnYiLOhEXdSIu6kRc1Im4tq6z/cyldt4U13dInRWdUeeI9j+y13W26lnsay/C/gbdfEtxfLH2mD9kDHq2z1/Isz11XoRxDTpnKPZTPCP2/BUviououLrsRnVq86+dB9fhrNNIsEudxX2h540tBmn39JxH9i3qdM5f/aLUeXoVn4qSFEf+lCkjTrV2fnG8+y8LQmg/o0Qf2EpjnezAsCHW2REXdSIu6kRc1Im4qBNx+f+vt1WKH+T5mI+yjeosPrdLnfR9cUfV2Y697/X569ROvNvjSVkiNwa1+fM5i1N55kdcWp1iWHd5svntpFQlTuKc03hKyzyIy1+nva/KZ7aDoE6UtV9dVhukTrRqrNNZT3Wdj7sP+VMa3yfictaZZpUk6Tfb2NjYvljYnJa4NiimbMyPcG6vT7fAa0VUNbTb69Pt9SlsnYmvkI4sfp0Y173O2+vTB3UiGupEXNSJuKgTcS3qrP50bDxLfIjP4CjL953UiSh61Qn0p9WpLQY6Vw6Nh4qD2vyJPzmjsfedYhZrj9Se7bX5i6+LK1tVZ3E3Kb4EdaJSxb5TRJ3oL/iRPWW77eqfKc5HPN+ZpK9OaolMmbSmNmf9xjgua6O1Ik9te86DU9puJbPX4ZjD+rhYZ0dc1Im4bm/P1Img3qkTYVEn4nr8e/aP3tegA1qJdWon5IFdiVf5ytdygAP469R67TLO3yggcF4hMc90u9v5XQzKee3jtSVpeRXrzF8O42qvUxykTnTw/vbSUucWR3PSxCdnnY/beTp9xxN14uH97eUeKNegQzjUibi+6nyhToQzq/MPdSIW6kRc1Imw/gPmo46VCzjLtAAAAABJRU5ErkJggg==" alt="" />  可以看出字典进行迭代操作时,得出的值不一定按顺序。

我们以前写Java和c++的程序员都习惯下标循环,那在Python中怎样实现呢?通过enumerate()函数即可实现。

根据Python官方文档,enumerate(sequence, start=0) 返回一个有默认下标从0开始的object。

 seasons = ['Spring', 'Summer', 'Fall', 'Winter']
print list(enumerate(seasons))
print list(enumerate(seasons, start=1))

Python学习笔记(三)——迭代的更多相关文章

  1. Python学习笔记014——迭代工具函数 内置函数enumerate()

    1 描述 enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中. 2 语法 enumerate(sequ ...

  2. Python 学习笔记三

    笔记三:函数 笔记二已取消置顶链接地址:http://www.cnblogs.com/dzzy/p/5289186.html 函数的作用: 給代码段命名,就像变量給数字命名一样 可以接收参数,像arg ...

  3. Python学习笔记(迭代、模块扩展、GUI 、编码处理等)

    PythonIDLE中的编码处理 http://www.tuicool.com/articles/NbyEBr 原文标题:Python中实际上已经得到了正确的Unicode或某种编码的字符,但是看起来 ...

  4. webdriver(python) 学习笔记三

    知识点:简单的对象定位 对象的定位应该是自动化测试的核心,要想操作一个对象,首先应该识别这个对象.一个对象就是一个人一样,他会有各种的特征(属性),如比我们可以通过一个人的身份证号,姓名,或者他住在哪 ...

  5. python学习笔记三--字典

    一.字典: 1. 不是序列,是一种映射, 键 :值的映射关系. 2. 没有顺序和位置的概念,只是把值存到对应的键里面. 3. 通过健而不是通过偏移量来读取 4. 任意对象的无序集合 5. 可变长,异构 ...

  6. python学习笔记(三)、字典

    字典是一种映射类型的数据类型.辣么什么是映射呢?如果看过<数据结构与算法>这一本书的小伙伴应该有印象(我也只是大学学习过,嘻嘻). 映射:就是将两个集合一 一对应起来,通过集合a的值,集合 ...

  7. python学习笔记(三)高级特性

    一.切片 list.tuple常常截取某一段元素,截取某一段元素的操作很常用 ,所以python提供了切片功能. L=['a','b','c','d','e','f'] #取索引0,到索引3的元素,不 ...

  8. python学习笔记三 文件操作(基础篇)

    文件操作 打开文件 open(name[,mode[,buffering]])   open函数使用一个文件名作为强制参数,然后返回一个文件对象.[python 3.5 把file()删除掉]   w ...

  9. python学习笔记三 深浅copy,扩展数据类型(基础篇)

    深浅copy以及赋值 对于字符串和数字而言,赋值.浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址. import copy n1 = #n1 = 'hahahaha' #赋值n2 = n1#浅co ...

  10. Python学习笔记三

    一. 为什么要使用函数? 函数可以方便阅读代码. 函数可以减少重复代码. 函数可以减少管理操作,减少修改操作. 二. 函数分类: 内置函数:len()   sum()   max()   min() ...

随机推荐

  1. 149. Best Time to Buy and Sell Stock【medium】

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  2. CSocket类网络编程 MFC

    Visual C++的MFC提供了CSocket类用来实现网络通信. 下面介绍VC++在Windows 95中实现Socket的 CSocket 类相关成员函数(这些成员函数实际上是从CAsyncSo ...

  3. css实现遮罩层(解决透明背景上的文字不透明)

    .PopUp_layer{   position:fixed;   top: 0;   left: 0;   right:0;   bottom:0;   width:100%;   height:1 ...

  4. 复习,关于server.xml的一点理解

    刚才在思考,我所写的一些java代码存放在了eclipse的一个workspace的目录下,这个目录下还包含我的web-project的所有html和jsp文件,以及/WEB-ROOT/WEB-INF ...

  5. 1326: The contest(并查集+分组背包)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1326 殷犇有很多队员.他们都认为自己是最强的,于是,一场比赛开始了~ 于是安叔主办了一场比赛,比赛 ...

  6. perl模块安装——cpanm

    下载 wget  http://xrl.us/cpanm mv cpanm.1 cpanm 配置 在bashrc或zshrc里面加入下面的几句话 最好把相对路径改成绝度路径(将~换成你根目录的绝对路径 ...

  7. 【C#】List<T>对象的深复制

    一.List对象中的T是值类型的情况(int 类型等) 对于值类型的List直接用以下方法就可以复制: List<T> oldList = new List<T>(); old ...

  8. rhel6.5 虚拟机的安装

    一.准备: 在网上下载 rhel-server-6.5-x86_64-dvd.iso 并在 非 C盘下创建一个目录 ,比如:E:\VM\rhel_test 二. 创建虚拟机 进入 VMware , 点 ...

  9. dom4j解析带命名空间的xml文件

    文件内容如下 <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd=& ...

  10. HTML——动画效果:图片循环横向播放

    一.html <!DOCTYPE HTML> <html> <head> <title>Home</title> <link href ...