<jupyter_start><jupyter_text>NumPy 1.6中引入的迭代器对象nditer提供了许多灵活的方式来系统地访问一个或多个数组的所有元素。本页介绍了一些基本的方法来使用该对象在Python中对数组进行计算，然后总结如何在Cython中加速内部循环。由于nditer的Python暴露是C数组迭代器API的相对直接的映射，因此这些想法​​也将为使用C或C ++进行数组迭代提供帮助。https://numpy.org/doc/stable/reference/arrays.nditer.html<jupyter_code>import numpy as np
a = np.arange(6).reshape(2,3)
for x in np.nditer(a):
    print(x, end=' ')

a = np.arange(6).reshape(2,3)
for x in np.nditer(a.T):
    print(x, end=' ')

for x in np.nditer(a.T.copy(order='C')):
    print(x, end=' ')

for x in np.nditer(a, order='F'):
    print(x, end=' ')

a.T,a.T.copy(order='C')
a = np.arange(6).reshape(2,3)

with np.nditer(a, op_flags=['readwrite']) as it:
   for x in it:
       x[...] = 2 * x

a
a = np.arange(6).reshape(2,3)
for x in np.nditer(a, flags=['external_loop']):
    print(x, end=' ')

for x in np.nditer(a, flags=['external_loop'], order='F'):
    print(x, end=' ')

a = np.arange(6).reshape(2,3)
it = np.nditer(a, flags=['f_index'])
for x in it:
    print("%d <%d>" % (x, it.index), end=' ')

print()

it = np.nditer(a, flags=['multi_index'])
for x in it:
    print("%d <%s>" % (x, it.multi_index), end=' ')<jupyter_output>0 <0> 1 <2> 2 <4> 3 <1> 4 <3> 5 <5> 
0 <(0, 0)> 1 <(0, 1)> 2 <(0, 2)> 3 <(1, 0)> 4 <(1, 1)> 5 <(1, 2)>
{'fraction_non_alphanumeric': 0.2254980079681275, 'fraction_numerical': 0.03824701195219123, 'mean_word_length': 4.322033898305085, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '621874', 'license': 'MIT', 'n_tokens_mistral': 682, 'n_tokens_neox': 678, 'n_words': 125, 'path': 'numpy/reference/05_arrays.nditer.ipynb', 'repo_name': 'kingreatwill/penter', 'size': '1255'}