Python 压缩两个 numpy 数组
Python zip on two numpy arrays
下面是一个工作示例,您可以复制所有变量和运行 for
循环来检查
我有两个列表,一个长度 = 13,一个长度 = 7。
varying_DA = [70,78,86,90,94,98,100,102,106,110,114,122,130]
test_array = [1,2,3,4,5,6,7,8,9,10,11,12,13] #DUMMY array
varying_Hinv = [60,76,92,100,108,124,140]
我想遍历以上两个列表,即使用两个 for
循环,如:
for vary_DA,test in zip(varying_DA,test_array):
for vary_Hinv,min_a,max_a in zip(varying_Hinv,min_aH,max_aH):
所以在上面的for
循环中,它从70
开始为vary_DA
循环遍历vary_Hinv
的7个值,即60,76,.. .,124,140。然后转到 78
for vary_DA
等等,直到它以 130
for vary_DA
.
结束
对于每个 vary_DA
和 vary_Hinv
值,我都有 min_aH
和 max_aH
的特定值。
min_aH = np.array([[24,30,30,30,30,30,30],[36,42,42,47,47,47,47],
[36,42,42,42,42,42,42],[36,42,42,42,42,42,42],
[42,47,47,47,53,47,53],[42,53,47,47,53,53,53],
[47,47,47,47,47,53,47],[47,47,47,47,47,59,59],
[53,53,59,59,59,59,64],[47,65,59,59,47,47,64],
[53,47,41,47,53,47,47],[36,41,41,41,53,47,47],
[36,36,41,41,53,47,47]])
# As you can see it is a 13x7 matrix, 7 for the 7 different vary_Hinv
# values and 13 for the 13 different vary_DA values
max_aH = np.array([[54,60,60,60,60,60,60],[60,66,66,66,66,66,66],
[66,66,72,72,72,72,72],[66,72,72,72,78,78,78],
[72,78,78,78,84,84,84],[78,84,84,89,89,84,84],
[78,84,78,89,84,84,78],[84,84,78,84,84,89,89],
[84,84,84,89,89,89,89],[84,89,84,89,101,101,101],
[89,89,95,101,95,101,101],[96,96,95,107,95,101,101],
[96,101,95,107,107,101,101]])
即当 vary_DA = 70
和 vary_Hinv = 60
、min_aH = 24
和 max_aH = 54
时。
当 vary_DA = 70
和 vary_Hinv = 76
、min_aH = 30
和 max_aH = 60
等等。
现在我想将 min_aH
和 max_aH
的这些特定值用于 for
循环,但是正如您从下面的 工作示例中看到的 它失败了。
ii = 0
for vary_DA,test in zip(varying_DA,test_array):
jj = 0
for vary_Hinv,min_a,max_a in zip(varying_Hinv,min_aH,max_aH):
print vary_DA,vary_Hinv,ii,jj,min_aH[ii][jj],max_aH[ii][jj]
jj = jj + 1
ii = ii + 1
IndexError: 标量变量索引无效。
底线:
当我可以清楚地做到以下几点时:
In [12]: min_aH[0]
Out[12]: array([24, 30, 30, 30, 30, 30, 30])
In [13]: min_aH[0][0]
Out[13]: 24
为什么在 for
循环中失败?我如何让它工作?
编辑:
我在上面的工作示例中犯了一个错误。在第二个 for
循环中,print
应该是:
print vary_DA,vary_Hinv,ii,jj,min_a[ii][jj],max_a[ii][jj]
我之前有 min_aH
和 max_aH
而不是 min_a
和 max_a
。如果以上方法有效,有人可以告诉我吗?
这适用于 Python 3.6:
varying_DA = [70,78,86,90,94,98,100,102,106,110,114,122,130]
test_array = [1,2,3,4,5,6,7,8,9,10,11,12,13] #DUMMY array
varying_Hinv = [60,76,92,100,108,124,140]
min_aH = np.array([[24,30,30,30,30,30,30],[36,42,42,47,47,47,47],
[36,42,42,42,42,42,42],[36,42,42,42,42,42,42],
[42,47,47,47,53,47,53],[42,53,47,47,53,53,53],
[47,47,47,47,47,53,47],[47,47,47,47,47,59,59],
[53,53,59,59,59,59,64],[47,65,59,59,47,47,64],
[53,47,41,47,53,47,47],[36,41,41,41,53,47,47],
[36,36,41,41,53,47,47]])
max_aH = np.array([[54,60,60,60,60,60,60],[60,66,66,66,66,66,66],
[66,66,72,72,72,72,72],[66,72,72,72,78,78,78],
[72,78,78,78,84,84,84],[78,84,84,89,89,84,84],
[78,84,78,89,84,84,78],[84,84,78,84,84,89,89],
[84,84,84,89,89,89,89],[84,89,84,89,101,101,101],
[89,89,95,101,95,101,101],[96,96,95,107,95,101,101],
[96,101,95,107,107,101,101]])
ii = 0
for vary_DA,test in zip(varying_DA,test_array):
jj = 0
for vary_Hinv,min_a,max_a in zip(varying_Hinv,min_aH,max_aH):
print(vary_DA,vary_Hinv,ii,jj,min_aH[ii][jj],max_aH[ii][jj])
jj = jj + 1
ii = ii + 1
一些评论,首先你的例子 运行s 对我来说没有错误使用 python 3,输出:
70 60 0 0 24 54
70 76 0 1 30 60
70 92 0 2 30 60
...
78 60 0 0 24 54
78 76 0 1 30 60
78 92 0 2 30 60
78 100 0 3 30 60
...
我认为这不是您想要的,因为最后四行的最后两个数字的值应该与前四行不同。我想你想要的最后四行是:
78 60 1 0 36 60
78 76 1 1 42 66
78 92 1 2 42 66
78 100 1 3 47 66
根据您的描述。这是因为
二 = 二 + 1
在您的初始循环之外,因此 ii 始终不会更改值。一个工作版本(再次出现在 python 3.6 中)将是:
ii = 0
for vary_DA,test in zip(varying_DA,test_array):
jj = 0
for vary_Hinv,min_a,max_a in zip(varying_Hinv,min_aH,max_aH):
print (vary_DA,vary_Hinv,ii,jj,min_aH[ii][jj],max_aH[ii][jj])
jj = jj + 1
ii = ii + 1
虽然这根本没有使用压缩值,但如果它试图达到预期的输出,将无法实现。你可以在这里看到,如果我打印出压缩值,你只是得到 min_aH 和 max_aH 的每个列表,但我相信你想要单独的值。
ii = 0
for vary_DA,test in zip(varying_DA,test_array):
jj = 0
for vary_Hinv,min_a,max_a in zip(varying_Hinv,min_aH,max_aH):
print (vary_DA,vary_Hinv,ii,jj,min_aH[ii][jj],max_aH[ii][jj],min_a,max_a)
jj = jj + 1
ii = ii + 1
结果:
70 60 0 0 24 54 [24 30 30 30 30 30 30] [54 60 60 60 60 60 60]
70 76 0 1 30 60 [36 42 42 47 47 47 47] [60 66 66 66 66 66 66]
70 92 0 2 30 60 [36 42 42 42 42 42 42] [66 66 72 72 72 72 72]
...
78 60 1 0 36 60 [24 30 30 30 30 30 30] [54 60 60 60 60 60 60]
78 76 1 1 42 66 [36 42 42 47 47 47 47] [60 66 66 66 66 66 66]
78 92 1 2 42 66 [36 42 42 42 42 42 42] [66 66 72 72 72 72 72]
...
最后正确使用 zip 来获取您想要的值,从而使迭代器 ii 和 jj 成为不必要的,将是:
ii = 0
for vary_DA,test,min_a,max_a in zip(varying_DA,test_array,min_aH,max_aH):
jj = 0
for vary_Hinv,mn,mx in zip(varying_Hinv,min_a,max_a):
print (vary_DA,vary_Hinv,ii,jj,min_aH[ii][jj],max_aH[ii][jj],mn,mx)
jj = jj + 1
ii = ii + 1
给予:
70 60 0 0 24 54 24 54
70 76 0 1 30 60 30 60
70 92 0 2 30 60 30 60
...
78 60 1 0 36 60 36 60
78 76 1 1 42 66 42 66
78 92 1 2 42 66 42 66
...
根据需要,最终删除迭代器,如下所示:
for vary_DA,test,min_a,max_a in zip(varying_DA,test_array,min_aH,max_aH):
for vary_Hinv,mn,mx in zip(varying_Hinv,min_a,max_a):
print (vary_DA,vary_Hinv,mn,mx)
这应该可以正常工作。
我也能够使用 python 2.7 成功地 运行 您的示例代码和我的响应,所以我不确定为什么您会根据此处的内容收到该错误。
下面是一个工作示例,您可以复制所有变量和运行 for
循环来检查
我有两个列表,一个长度 = 13,一个长度 = 7。
varying_DA = [70,78,86,90,94,98,100,102,106,110,114,122,130]
test_array = [1,2,3,4,5,6,7,8,9,10,11,12,13] #DUMMY array
varying_Hinv = [60,76,92,100,108,124,140]
我想遍历以上两个列表,即使用两个 for
循环,如:
for vary_DA,test in zip(varying_DA,test_array):
for vary_Hinv,min_a,max_a in zip(varying_Hinv,min_aH,max_aH):
所以在上面的for
循环中,它从70
开始为vary_DA
循环遍历vary_Hinv
的7个值,即60,76,.. .,124,140。然后转到 78
for vary_DA
等等,直到它以 130
for vary_DA
.
对于每个 vary_DA
和 vary_Hinv
值,我都有 min_aH
和 max_aH
的特定值。
min_aH = np.array([[24,30,30,30,30,30,30],[36,42,42,47,47,47,47],
[36,42,42,42,42,42,42],[36,42,42,42,42,42,42],
[42,47,47,47,53,47,53],[42,53,47,47,53,53,53],
[47,47,47,47,47,53,47],[47,47,47,47,47,59,59],
[53,53,59,59,59,59,64],[47,65,59,59,47,47,64],
[53,47,41,47,53,47,47],[36,41,41,41,53,47,47],
[36,36,41,41,53,47,47]])
# As you can see it is a 13x7 matrix, 7 for the 7 different vary_Hinv
# values and 13 for the 13 different vary_DA values
max_aH = np.array([[54,60,60,60,60,60,60],[60,66,66,66,66,66,66],
[66,66,72,72,72,72,72],[66,72,72,72,78,78,78],
[72,78,78,78,84,84,84],[78,84,84,89,89,84,84],
[78,84,78,89,84,84,78],[84,84,78,84,84,89,89],
[84,84,84,89,89,89,89],[84,89,84,89,101,101,101],
[89,89,95,101,95,101,101],[96,96,95,107,95,101,101],
[96,101,95,107,107,101,101]])
即当 vary_DA = 70
和 vary_Hinv = 60
、min_aH = 24
和 max_aH = 54
时。
当 vary_DA = 70
和 vary_Hinv = 76
、min_aH = 30
和 max_aH = 60
等等。
现在我想将 min_aH
和 max_aH
的这些特定值用于 for
循环,但是正如您从下面的 工作示例中看到的 它失败了。
ii = 0
for vary_DA,test in zip(varying_DA,test_array):
jj = 0
for vary_Hinv,min_a,max_a in zip(varying_Hinv,min_aH,max_aH):
print vary_DA,vary_Hinv,ii,jj,min_aH[ii][jj],max_aH[ii][jj]
jj = jj + 1
ii = ii + 1
IndexError: 标量变量索引无效。
底线: 当我可以清楚地做到以下几点时:
In [12]: min_aH[0]
Out[12]: array([24, 30, 30, 30, 30, 30, 30])
In [13]: min_aH[0][0]
Out[13]: 24
为什么在 for
循环中失败?我如何让它工作?
编辑:
我在上面的工作示例中犯了一个错误。在第二个 for
循环中,print
应该是:
print vary_DA,vary_Hinv,ii,jj,min_a[ii][jj],max_a[ii][jj]
我之前有 min_aH
和 max_aH
而不是 min_a
和 max_a
。如果以上方法有效,有人可以告诉我吗?
这适用于 Python 3.6:
varying_DA = [70,78,86,90,94,98,100,102,106,110,114,122,130]
test_array = [1,2,3,4,5,6,7,8,9,10,11,12,13] #DUMMY array
varying_Hinv = [60,76,92,100,108,124,140]
min_aH = np.array([[24,30,30,30,30,30,30],[36,42,42,47,47,47,47],
[36,42,42,42,42,42,42],[36,42,42,42,42,42,42],
[42,47,47,47,53,47,53],[42,53,47,47,53,53,53],
[47,47,47,47,47,53,47],[47,47,47,47,47,59,59],
[53,53,59,59,59,59,64],[47,65,59,59,47,47,64],
[53,47,41,47,53,47,47],[36,41,41,41,53,47,47],
[36,36,41,41,53,47,47]])
max_aH = np.array([[54,60,60,60,60,60,60],[60,66,66,66,66,66,66],
[66,66,72,72,72,72,72],[66,72,72,72,78,78,78],
[72,78,78,78,84,84,84],[78,84,84,89,89,84,84],
[78,84,78,89,84,84,78],[84,84,78,84,84,89,89],
[84,84,84,89,89,89,89],[84,89,84,89,101,101,101],
[89,89,95,101,95,101,101],[96,96,95,107,95,101,101],
[96,101,95,107,107,101,101]])
ii = 0
for vary_DA,test in zip(varying_DA,test_array):
jj = 0
for vary_Hinv,min_a,max_a in zip(varying_Hinv,min_aH,max_aH):
print(vary_DA,vary_Hinv,ii,jj,min_aH[ii][jj],max_aH[ii][jj])
jj = jj + 1
ii = ii + 1
一些评论,首先你的例子 运行s 对我来说没有错误使用 python 3,输出:
70 60 0 0 24 54
70 76 0 1 30 60
70 92 0 2 30 60
...
78 60 0 0 24 54
78 76 0 1 30 60
78 92 0 2 30 60
78 100 0 3 30 60
...
我认为这不是您想要的,因为最后四行的最后两个数字的值应该与前四行不同。我想你想要的最后四行是:
78 60 1 0 36 60
78 76 1 1 42 66
78 92 1 2 42 66
78 100 1 3 47 66
根据您的描述。这是因为 二 = 二 + 1 在您的初始循环之外,因此 ii 始终不会更改值。一个工作版本(再次出现在 python 3.6 中)将是:
ii = 0
for vary_DA,test in zip(varying_DA,test_array):
jj = 0
for vary_Hinv,min_a,max_a in zip(varying_Hinv,min_aH,max_aH):
print (vary_DA,vary_Hinv,ii,jj,min_aH[ii][jj],max_aH[ii][jj])
jj = jj + 1
ii = ii + 1
虽然这根本没有使用压缩值,但如果它试图达到预期的输出,将无法实现。你可以在这里看到,如果我打印出压缩值,你只是得到 min_aH 和 max_aH 的每个列表,但我相信你想要单独的值。
ii = 0
for vary_DA,test in zip(varying_DA,test_array):
jj = 0
for vary_Hinv,min_a,max_a in zip(varying_Hinv,min_aH,max_aH):
print (vary_DA,vary_Hinv,ii,jj,min_aH[ii][jj],max_aH[ii][jj],min_a,max_a)
jj = jj + 1
ii = ii + 1
结果:
70 60 0 0 24 54 [24 30 30 30 30 30 30] [54 60 60 60 60 60 60]
70 76 0 1 30 60 [36 42 42 47 47 47 47] [60 66 66 66 66 66 66]
70 92 0 2 30 60 [36 42 42 42 42 42 42] [66 66 72 72 72 72 72]
...
78 60 1 0 36 60 [24 30 30 30 30 30 30] [54 60 60 60 60 60 60]
78 76 1 1 42 66 [36 42 42 47 47 47 47] [60 66 66 66 66 66 66]
78 92 1 2 42 66 [36 42 42 42 42 42 42] [66 66 72 72 72 72 72]
...
最后正确使用 zip 来获取您想要的值,从而使迭代器 ii 和 jj 成为不必要的,将是:
ii = 0
for vary_DA,test,min_a,max_a in zip(varying_DA,test_array,min_aH,max_aH):
jj = 0
for vary_Hinv,mn,mx in zip(varying_Hinv,min_a,max_a):
print (vary_DA,vary_Hinv,ii,jj,min_aH[ii][jj],max_aH[ii][jj],mn,mx)
jj = jj + 1
ii = ii + 1
给予:
70 60 0 0 24 54 24 54
70 76 0 1 30 60 30 60
70 92 0 2 30 60 30 60
...
78 60 1 0 36 60 36 60
78 76 1 1 42 66 42 66
78 92 1 2 42 66 42 66
...
根据需要,最终删除迭代器,如下所示:
for vary_DA,test,min_a,max_a in zip(varying_DA,test_array,min_aH,max_aH):
for vary_Hinv,mn,mx in zip(varying_Hinv,min_a,max_a):
print (vary_DA,vary_Hinv,mn,mx)
这应该可以正常工作。
我也能够使用 python 2.7 成功地 运行 您的示例代码和我的响应,所以我不确定为什么您会根据此处的内容收到该错误。