根据数组中的值拆分NumPy数组(条件) [英] Split NumPy array according to values in the array (a condition)
本文介绍了根据数组中的值拆分NumPy数组(条件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组:
arr = [(1,1,1), (1,1,2), (1,1,3), (1,1,4)...(35,1,22),(35,1,23)]
我想根据每个有序对中的第三个值分割数组.我希望每个1的第三个值都作为开始 一个新数组.结果应该是:
I want to split my array according to the third value in each ordered pair. I want each third value of 1 to be the start of a new array. The results should be:
[(1,1,1), (1,1,2),...(1,1,35)][(1,2,1), (1,2,2),...(1,2,46)]
,依此类推.我知道numpy.split应该可以解决问题,但我不知道如何编写拆分条件.
and so on. I know numpy.split should do the trick but I'm lost as to how to write the condition for the split.
推荐答案
我想不出任何numpy
函数或技巧来做到这一点.使用for循环的简单解决方案是-
I cannot think of any numpy
functions or tricks to do this . A simple solution using for loop would be -
In [48]: arr = [(1,1,1), (1,1,2), (1,1,3), (1,1,4),(1,2,1),(1,2,2),(1,2,3),(1,3,1),(1,3,2),(1,3,3),(1,3,4),(1,3,5)]
In [49]: result = []
In [50]: for i in arr:
....: if i[2] == 1:
....: tempres = []
....: result.append(tempres)
....: tempres.append(i)
....:
In [51]: result
Out[51]:
[[(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4)],
[(1, 2, 1), (1, 2, 2), (1, 2, 3)],
[(1, 3, 1), (1, 3, 2), (1, 3, 3), (1, 3, 4), (1, 3, 5)]]
这篇关于根据数组中的值拆分NumPy数组(条件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文