Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从一维列表创建多维数组,numpy_Python_Arrays_Numpy_Multidimensional Array_Indexing - Fatal编程技术网

Python 从一维列表创建多维数组,numpy

Python 从一维列表创建多维数组,numpy,python,arrays,numpy,multidimensional-array,indexing,Python,Arrays,Numpy,Multidimensional Array,Indexing,努力用语言描述这个问题,但有一个看似简单的问题我找不到答案 我想使用一个列表/数组中的值和另一个列表/数组中的索引创建一个数组。我希望新数组的形状与索引数组相同 import numpy as np a = np.array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2]) b = [[0, 1], [2, 3, 4], [6, 7, 8, 9, 10]] result = func(a, b) #some function or operator... p

努力用语言描述这个问题,但有一个看似简单的问题我找不到答案

我想使用一个列表/数组中的值和另一个列表/数组中的索引创建一个数组。我希望新数组的形状与索引数组相同

import numpy as np

a = np.array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2])

b = [[0, 1], [2, 3, 4], [6, 7, 8, 9, 10]]

result = func(a, b) #some function or operator...

print(result)
[[9, 8], [7, 6, 5], [3, 2, 1, 0, -1]]
谢谢!:)

编辑:


到目前为止,这是一个很好的解决方案,但我更愿意在不使用for循环的情况下实现这一点,因为我们正在查看成百上千行,并且需要缩短计算时间。再次感谢:)

您可以使用列表:

>>> [a[x[0]:x[-1]+1] for x in b]
[array([9, 8]), array([7, 6, 5]), array([ 3,  2,  1,  0, -1])]
编辑:您的问题表明您需要一个更快的选项,因此您可以测试以下脚本,看看哪一个对于Python安装更快:

#!/usr/bin/env python                                                               

import timeit

setup = '''                                                                         
import numpy as np                                                                  

a = np.array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2])                                
b = [[0, 1], [2, 3, 4], [6, 7, 8, 9, 10]]                                           
'''

test1 = '''                                                                         
def test():                                                                         
  return [a[x[0]:x[-1]+1] for x in b]                                               
'''

test2 = '''                                                                         
def test():                                                                         
  return [a[idx] for idx in b]                                                      
'''

print(timeit.timeit(setup = setup,
                    stmt = test1,
                    number = 1000000))

print(timeit.timeit(setup = setup,
                    stmt = test2,
                    number = 1000000))

在我的机器上,到目前为止,您使用的两种方法大致相同,但hpaulj的速度可能会稍微快一点(除非Python在后台缓存数据),这在生产中可能更有用。在本地进行测试,看看您得到的答案是否相似或不同。

只需将每个索引子列表应用于
a

In [483]: a = np.array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2])
     ...: 
     ...: b = [[0, 1], [2, 3, 4], [6, 7, 8, 9, 10]]
     ...: 
     ...: 
In [484]: [a[idx] for idx in b]
Out[484]: [array([9, 8]), array([7, 6, 5]), array([ 3,  2,  1,  0, -1])]
子列表的长度不同,因此无法将结果生成2d数组-它必须保留为列表(或者如果坚持使用1d对象数据类型数组)