Python中的迭代Iteration

Python中的迭代

迭代器种类

1. enumerate()
2. items()
3. np.nditer()
4. iterrows()

不同迭代器之间的区别:

迭代器的名称 迭代器的特点
enumerate() 迭代的同时生成索引信息
items() 用于字典同时迭代生成关键字和对应内容
np.nditer() 用于迭代一个numpy多维数组(认先行后列)
iterows() 用于迭代一个pandas数组

使用迭代器的实例

enumerate()

# Input:
l = ['a', 'b', 'c']
for index, item in enumerate(l):
    print(index, ':', item)

# Output:
0 : a
1 : b
2 : c

items()

# Input:
dict = {'name:': 'xiaoming', 'grade': '59'}
for key, item in dict.items():
    print(key, ':', item)

# Output:
name: : xiaoming
grade : 59

np.nditer()

# Input:
a = np.array([[1, 2], [4, 5]])
it = np.nditer(a1, flags=['multi_index'], op_flags=['readwrite'])
while not it.finished:
    ix = it.multi_index
    a[ix] += 100
    it.iternext()

# Output:
a
array([[101, 102],
       [104, 105]])

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...