Python 在不使用nditer的情况下,用numpy迭代两个数组?

Python 在不使用nditer的情况下,用numpy迭代两个数组?,python,arrays,multidimensional-array,numpy,Python,Arrays,Multidimensional Array,Numpy,考虑numpy数组的规范,该规范通常用于指定matplotlib打印数据: t = np.arange(0.0,1.5,0.25) s = np.sin(2*np.pi*t) 基本上,它存储数组中t中(x,y)数据点的x坐标;以及数组s中得到的y坐标(y=f(x)的结果,在本例中为sin(x))。然后,使用numpy.nditer函数可以非常方便地获得t和s中的连续条目对,表示数据点的(x,y)坐标,如下所示: for x, y in np.nditer([t,s]): print("x

考虑
numpy
数组的规范,该规范通常用于指定
matplotlib
打印数据:

t = np.arange(0.0,1.5,0.25)
s = np.sin(2*np.pi*t) 
基本上,它存储数组中
t
(x,y)
数据点的
x
坐标;以及数组
s
中得到的
y
坐标(y=f(x)的结果,在本例中为
sin(x)
)。然后,使用
numpy.nditer
函数可以非常方便地获得
t
s
中的连续条目对,表示数据点的
(x,y)
坐标,如下所示:

for x, y in np.nditer([t,s]):
  print("xy: %f:%f" % (x,y))
因此,我尝试以下代码片段作为
test.py

import numpy as np
print("numpy version {0}".format(np.__version__))
t = np.arange(0.0,1.5,0.25)   ; print("t", ["%+.2e"%i for i in t])
s = np.sin(2*np.pi*t)         ; print("s", ["%+.2e"%i for i in s])
print("i", ["% 9d"%i for i in range(0, len(t))])
for x, y in np.nditer([t,s]):
  print("xy: %f:%f" % (x,y))
。。。结果是:

$ python3.2 test.py 
numpy version 1.7.0
t ['+0.00e+00', '+2.50e-01', '+5.00e-01', '+7.50e-01', '+1.00e+00', '+1.25e+00']
s ['+0.00e+00', '+1.00e+00', '+1.22e-16', '-1.00e+00', '-2.45e-16', '+1.00e+00']
i ['        0', '        1', '        2', '        3', '        4', '        5']
xy: 0.000000:0.000000
xy: 0.250000:1.000000
xy: 0.500000:0.000000
xy: 0.750000:-1.000000
xy: 1.000000:-0.000000
xy: 1.250000:1.000000

$ python2.7 test.py 
numpy version 1.5.1
('t', ['+0.00e+00', '+2.50e-01', '+5.00e-01', '+7.50e-01', '+1.00e+00', '+1.25e+00'])
('s', ['+0.00e+00', '+1.00e+00', '+1.22e-16', '-1.00e+00', '-2.45e-16', '+1.00e+00'])
('i', ['        0', '        1', '        2', '        3', '        4', '        5'])
Traceback (most recent call last):
  File "test.py", line 10, in <module>
    for x, y in np.nditer([t,s]):
AttributeError: 'module' object has no attribute 'nditer'
。。。这似乎很管用

$ python2.7 test.py 
numpy version 1.5.1
('t', ['+0.00e+00', '+2.50e-01', '+5.00e-01', '+7.50e-01', '+1.00e+00', '+1.25e+00'])
('s', ['+0.00e+00', '+1.00e+00', '+1.22e-16', '-1.00e+00', '-2.45e-16', '+1.00e+00'])
('i', ['        0', '        1', '        2', '        3', '        4', '        5'])
xyIt: 0.000000:0.000000
xyIt: 0.250000:1.000000
xyIt: 0.500000:0.000000
xyIt: 0.750000:-1.000000
xyIt: 1.000000:-0.000000
xyIt: 1.250000:1.000000
Traceback (most recent call last):
  File "test.py", line 23, in <module>
    for x, y in np.nditer([t,s]):
AttributeError: 'module' object has no attribute 'nditer'
$ python3.2 test.py 
numpy version 1.7.0
t ['+0.00e+00', '+2.50e-01', '+5.00e-01', '+7.50e-01', '+1.00e+00', '+1.25e+00']
s ['+0.00e+00', '+1.00e+00', '+1.22e-16', '-1.00e+00', '-2.45e-16', '+1.00e+00']
i ['        0', '        1', '        2', '        3', '        4', '        5']
xyIt: 0.000000:0.000000
xyIt: 0.250000:1.000000
xyIt: 0.500000:0.000000
xyIt: 0.750000:-1.000000
xyIt: 1.000000:-0.000000
xyIt: 1.250000:1.000000
xynd: 0.000000:0.000000
xynd: 0.250000:1.000000
xynd: 0.500000:0.000000
xynd: 0.750000:-1.000000
xynd: 1.000000:-0.000000
xynd: 1.250000:1.000000
$python2.7 test.py
numpy版本1.5.1
(‘t’、[‘0.00e+00’、‘2.50e-01’、‘5.00e-01’、‘7.50e-01’、‘1.00e+00’、‘1.25e+00’)
(“s”、[“+0.00e+00”、“+1.00e+00”、“+1.22e-16”、“-1.00e+00”、“-2.45e-16”、“+1.00e+00”])
('i',['0','1','2','3','4','5'])
xyIt:0.000000:0.000000
xyIt:0.250000:1.000000
xyIt:0.500000:0.000000
xyIt:0.750000:-1.000000
xyIt:1.000000:-0.000000
xyIt:1.250000:1.000000
回溯(最近一次呼叫最后一次):
文件“test.py”,第23行,在
对于np.nditer([t,s])中的x,y:
AttributeError:“模块”对象没有属性“nditer”
$python3.2 test.py
numpy版本1.7.0
t['+0.00e+00'、'+2.50e-01'、'+5.00e-01'、'+7.50e-01'、'+1.00e+00'、'+1.25e+00']
s['+0.00e+00'、'+1.00e+00'、'+1.22e-16'、'-1.00e+00'、'-2.45e-16'、'+1.00e+00']
i['0'、'1'、'2'、'3'、'4'、'5']
xyIt:0.000000:0.000000
xyIt:0.250000:1.000000
xyIt:0.500000:0.000000
xyIt:0.750000:-1.000000
xyIt:1.000000:-0.000000
xyIt:1.250000:1.000000
xynd:0.000000:0.000000
xynd:0.250000:1.000000
xynd:0.500000:0.000000
xynd:0.750000:-1.000000
xynd:1.000000:-0.000000
xynd:1.250000:1.000000
我的问题是——在numpy<1.6.0版本中,这种迭代应该这样做吗?

将两个向量合并到一个数组中如何:

for x,y in np.c_[t,s]:
    print("xy: %f:%f" % (x,y))
这给

xy: 0.000000:0.000000
xy: 0.250000:1.000000
xy: 0.500000:0.000000
xy: 0.750000:-1.000000
xy: 1.000000:-0.000000
xy: 1.250000:1.000000
如果要进行迭代以节省内存,可以使用以下函数:

for x,y in itertools.izip(t,s):
    print("xy: %f:%f" % (x,y))
邮政编码为x,y(t,s):。对于1d阵列,它真的很简单

已验证可在Python 2和Python 3中工作。zip()返回Python2上的列表,正如DiggyF所建议的,
itertools.izip()
可能更适合大型数组

对于>1D数组,迭代将通过最后一个维度返回(N-1)D数组。如果您必须处理N-d阵列,这可能是您想要的,也可能不是您想要的

无论如何,它无疑是可移植的,在数组对象上迭代是为了支持这种用例:)

这对我来说很有效

x = x.flatten()
y = y.flatten()
for i,j in zip(x,y):
     #do something
x = x.flatten()
y = y.flatten()
for i,j in zip(x,y):
     #do something