numpy Python/数字:将非连续值拆分为离散子集数组

2vuwiymt  于 2022-11-23  发布在  Python
关注(0)|答案(2)|浏览(159)

如何将这样的数组分割成n个子集,其中一个子集由连续的值组成?
第一个

whhtz7ly

whhtz7ly1#

您可以使用np.split,并且只传入cut_points作为第二个参数。
例如

split_arr = np.split(arr, cut_points)

# split_arr looks like:
# [array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14]),
# array([39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55]),
# array([66, 67, 68, 69, 70, 71])]

完整解决方案:

import numpy as np
arr = np.array((0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 39, 40,
       41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 66, 67, 68, 69, 70, 71))
cut_points = np.where(np.diff(arr) != 1)[0] + 1
split_arr = np.split(arr, split_points)
split_arr
# outputs:
[array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14]),
 array([39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55]),
 array([66, 67, 68, 69, 70, 71])]
xuo3flqw

xuo3flqw2#

只是作为一种替代方式,没有Pandas/麻木。
如果你不关心输入/输出的顺序,你可以从最后开始,做如下的事情:

l = (0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 66, 67, 68, 69, 70, 71)
i = 0
current_index = -1
prev_value = None
result = []
for k in l[::-1]:
    current_value = k + i
    if prev_value != current_value:
        prev_value = current_value
        current_index += 1
        result.append([])
    result[current_index].append(k)
    i += 1
print(result)

则结果将包含:

[
    [71, 70, 69, 68, 67, 66],
    [55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39],
    [14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
]

相关问题