array([ 0, 1, 2, 3, 2, 1, 0, -2, -3, -5, -7, -9, -8, -6, -5, -3, -1, 2, 3, 5, 7, 8, 6, 5, 7, 9, 11, 12, 14, 16, 13, 11, 9, 8, 7, 5, 4, 2, -1, -3, -6, -8, -11, -13, -16, -18]) 例如把以上数组划分成 4 个数组放进列表里 [array([ 0, 1, 2, 3, 2, 1, 0]),array([ -2, -3, -5, -7, -9, -8, -6, -5, -3, -1]),array([ 2, 3, 5, 7, 8, 6, 5, 7, 9, 11, 12, 14, 16, 13, 11, 9, 8, 7, 5, 4, 2]),array([ -1, -3, -6, -8, -11, -13, -16, -18])]
1
ipwx 2018-06-21 23:33:49 +08:00 via iPhone 1
np.where
|
2
msg7086 2018-06-22 04:59:15 +08:00 1
a = [0, 1, 2, 3, 2, 1, 0, -2, -3, -5, -7, -9, -8, -6, -5, -3, -1, 2, 3, 5, 7, 8, 6, 5, 7, 9, 11, 12, 14, 16, 13, 11, 9, 8, 7, 5, 4, 2, -1, -3, -6, -8, -11, -13, -16, -18]
r = [] a.each_with_index do |n, i| r << [] if i == 0 || a[i] >= 0 && a[i-1] < 0 || a[i] < 0 && a[i-1] >= 0 r.last << n end r => [[0, 1, 2, 3, 2, 1, 0], [-2, -3, -5, -7, -9, -8, -6, -5, -3, -1], [2, 3, 5, 7, 8, 6, 5, 7, 9, 11, 12, 14, 16, 13, 11, 9, 8, 7, 5, 4, 2], [-1, -3, -6, -8, -11, -13, -16, -18]] |