1、给定一个集合list或者tuple,可以通过for …… in ……的语法来实现循环遍历,这个循环我们就叫做迭代

迭代list:

>>> m = ['haha','hehe','heihei','gaga']
>>> for li in m:
... print(li)
...
haha
hehe
heihei
gaga

迭代字符串:

>>> n = 'abcdefg'
>>> for str in n :
... print(str)
...
a
b
c
d
e
f
g

迭代dict,迭代key

>>> l = {'name':'wuchong','age':15}
>>> for dic in l:
... print(dic)
...
name
age

迭代value:

>>> for dic in l.values():
... print(dic)
...
wuchong
15

同时迭代key、value:

>>> for key,value in l.items():
... print(key,value)
...
name wuchong
age 15

Python中,只要是可迭代对象,都可以迭代。

那么,如何判断一个对象是不是可迭代对象呢?方法是通过collections中的Iterable类型判断。

>>> from collections import Iterable
>>> isinstance('',Iterable)
True
>>> isinstance([1,2,3],Iterable)
True
>>> isinstance(123,Iterable)
False

可知,整数类型不是可迭代类型。

通过Python内置的enumerate函数可以把一个list变成索引-元素对的形式:

>>> m
['haha', 'hehe', 'heihei', 'gaga']
>>> for dx,value in enumerate(m):
... print(dx,value)
...
0 haha
1 hehe
2 heihei
3 gaga

练习:使用迭代从一个list中查询最大值和最小值,如果list为None,返回[None,None],并返回一个tuple

>>> def find(l):
... if l==[]:
... return (None,None)
... else:
... min = max = l[0]
... for i in l:
... if i<min :
... min = i
... if i>max:
... max = i
... return (min,max)
>>> l=[3,6,4,8,1,0]
>>> find(l)
(0, 8)
>>> l=[]
>>> find(l)
(None, None)

Python高级特性——迭代(Iteration)的更多相关文章

  1. Day8 python高级特性-- 迭代 Iteration

    通过for循环来遍历 list.tuple.dict.甚至是字符串,这种遍历被称为迭代. 相比于C的for循环, Python的for循环抽象成都更好,不仅可以用在list或tuple上,还可以用在其 ...

  2. python高级特性-迭代

    概述 for  v   in d.values(): for k,v  in d.items(): for  a    in 'adfa': #判断对象是否可迭代 from collections i ...

  3. 三、python高级特性(切片、迭代、列表生成器、生成器)

    1.python高级特性 1.1切片 list列表 L=['Mli','add','sal','saoo','Lkkl'] L[0:3]  #即为['Mli','add','sal']  从索引0开始 ...

  4. python高级特性:切片/迭代/列表生成式/生成器

    廖雪峰老师的教程上学来的,地址:python高级特性 下面以几个具体示例演示用法: 一.切片 1.1 利用切片实现trim def trim(s): while s[:1] == " &qu ...

  5. Python 高级特性介绍 - 迭代的99种姿势 与协程

    Python 高级特性介绍 - 迭代的99种姿势 与协程 引言 写这个笔记记录一下一点点收获 测试环境版本: Python 3.7.4 (default, Sep 28 2019, 16:39:19) ...

  6. Python高级特性(1):Iterators、Generators和itertools(转)

    译文:Python高级特性(1):Iterators.Generators和itertools [译注]:作为一门动态脚本语言,Python 对编程初学者而言很友好,丰富的第三方库能够给使用者带来很大 ...

  7. Python高级特性之:List Comprehensions、Generator、Dictionary and set ...

    今天帅气的易哥和大家分享的是Pyton的高级特性,希望大家能和我一起学习这门语言的魅力. Python高级特性之:List Comprehensions.Generator.Dictionary an ...

  8. python高级特性和高阶函数

    python高级特性 1.集合的推导式 列表推导式,使用一句表达式构造一个新列表,可包含过滤.转换等操作. 语法:[exp for item in collection if codition] if ...

  9. Python高级特性(3): Classes和Metaclasses(转)

    原文:Python高级特性(3): Classes和Metaclasses 类和对象 类和函数一样都是Python中的对象.当一个类定义完成之后,Python将创建一个“类对象”并将其赋值给一个同名变 ...

随机推荐

  1. 小白学 Python 爬虫(14):urllib 基础使用(四)

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  2. 开发者如何学好 MongoDB

    作为一名研发,数据库是或多或少都会接触到的技术. MongoDB 是当前火热的 NoSQL 之一,我们怎样才能学好 MongoDB 呢?本篇文章,我们将从以下几方面讨论这个话题: MongoDB 是什 ...

  3. 转:OAuth2 深入介绍

    OAuth2 深入介绍 1. 前言 2. OAuth2 角色 2.1 资源所有者(Resource Owner) 2.2 资源/授权服务器(Resource/Authorization Server) ...

  4. [Mathematics][BJTU][Calculus]Detailed explanations and proofs of the Dirac-Abel Discriminant Methods which deal with the conditional convergence

    So, today we will talk about the conditional convergence and two discriminant methods, namely Dirac- ...

  5. luogu P4408 [NOI2003]逃学的小孩

    题目描述 Chris家的电话铃响起了,里面传出了Chris的老师焦急的声音:"喂,是Chris的家长吗?你们的孩子又没来上课,不想参加考试了吗?"一听说要考试,Chris的父母就心 ...

  6. Undefined symbols for architecture x86_64"_OBJC_CLASS_$_QQApiInterface 怎么搞

    今天上午报了一个这样的错误 解决办法 如此如此 ~~ 然后编译 看看报的什么错误 还是不行的话就重新导入三方库 添加依赖库 结果build success

  7. git 使用详解(7)-- 远程仓库的使用

    要参与任何一个 Git 项目的协作,必须要了解该如何管理远程仓库.远程仓库是指 托管在网络上 的项目仓库,可能会有好多个,其中有些你 只能读,另外有些可以写.同他人协作开发某个项目时,需要管理这些远程 ...

  8. jQuery操作元素对象的样式

    在jQuery中操作元素为了加快速度,或者书写速度,可以用到json的格式: <!DOCTYPE html> <html> <head> <meta char ...

  9. nbuoj2784 倒水

    题目:http://www.nbuoj.com/v8.83/Problems/Problem.php?pid=2784 一天,TJ买了N个容量无限大的瓶子,开始时每个瓶子里有1升水.接着TJ决定只保留 ...

  10. ARTS-S C语言多线程传参数

    #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <unistd.h& ...