如何在不相等的部分中拆分数组?

时间:2018-02-01 11:35:56

标签: python arrays numpy

我有一个像这样的数组

import numpy as np

x = np.array([1, 2, 3, 99, 99, 3, 2, 1])

我想把它分成三部分

x1 = array([1, 2, 3])
x2 = array([99, 99])
x3 = array([3, 2, 1])

最好的方法是什么?

1 个答案:

答案 0 :(得分:5)

您可以使用np.split

/**
 * Implements template_process_html().
 * Remove Query Strings from CSS & JS filenames
 */
function THEMENAME_process_html( &$variables) {
    $variables['styles'] = preg_replace('/\.css\?[^"]+/', '.css', $variables['styles']);
    $variables['scripts'] = preg_replace('/\.js\?[^"]+/', '.js', $variables['scripts']);
}

/**
 * Implement hook_image_style
 * Override theme image style to remove query string.
 * @param $variables
 */
function THEMENAME_image_style($variables) {
    // Determine the dimensions of the styled image.
    $dimensions = array(
        'width' => $variables['width'],
        'height' => $variables['height'],
    );
    image_style_transform_dimensions($variables['style_name'], $dimensions);
    $variables['width'] = $dimensions['width'];
    $variables['height'] = $dimensions['height'];
    // Determine the URL for the styled image.
    $variables['path'] = image_style_url($variables['style_name'], $variables['path']);
    // Remove query string for image.
    $variables['path'] = preg_replace('/\?.*/', '', $variables['path']);
    return theme('image', $variables);
}

x = np.array([1, 2, 3, 99, 99, 3, 2, 1]) x1, x2, x3 = np.split(x, [3, 5]) 从而指定要拆分的索引。

产生

[3, 5]