1. enumerate()
2. items()
3. np.nditer()
4. iterrows()
迭代器的名称 | 迭代器的特点 |
---|---|
enumerate() | 迭代的同时生成索引信息 |
items() | 用于字典同时迭代生成关键字和对应内容 |
np.nditer() | 用于迭代一个numpy多维数组(默认先行后列) |
iterows() | 用于迭代一个pandas数组 |
# Input:
l = [‘a‘, ‘b‘, ‘c‘]
for index, item in enumerate(l):
print(index, ‘:‘, item)
# Output:
0 : a
1 : b
2 : c
# Input:
dict = {‘name:‘: ‘xiaoming‘, ‘grade‘: ‘59‘}
for key, item in dict.items():
print(key, ‘:‘, item)
# Output:
name: : xiaoming
grade : 59
# 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]])
原文:https://www.cnblogs.com/musicalife/p/14725627.html