Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.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.array和标量的组合_Python_Numpy - Fatal编程技术网

Python 元素与numpy.array和标量的组合

Python 元素与numpy.array和标量的组合,python,numpy,Python,Numpy,我有一个tuple,它包含一个任意长度的numpy.array,以及标量。大概是这样的: (array([ 31.5, 31.6, 31.7, 31.8, 31.9, 32. , 32.1, 32.2, 32.3, 32.4, 32.5, 32.6, 32.7, 32.8, 32.9, 33. , 33.1, 33.2, 33.3, 33.4, 33.5, 33.6, 33.7, 33.8, 33.9, 34. , 34.1,

我有一个
tuple
,它包含一个任意长度的
numpy.array
,以及标量。大概是这样的:

(array([ 31.5,  31.6,  31.7,  31.8,  31.9,  32. ,  32.1,  32.2,  32.3,
    32.4,  32.5,  32.6,  32.7,  32.8,  32.9,  33. ,  33.1,  33.2,
    33.3,  33.4,  33.5,  33.6,  33.7,  33.8,  33.9,  34. ,  34.1,
    34.2,  34.3,  34.4,  34.5,  34.6,  34.7,  34.8,  34.9,  35. ,
    35.1,  35.2]), 30.0, 0.0025, 0.0, 0.0027, 0.2791, 1.5, 1.0, 100.0)
(array([31.5, 30.0, 0.0025, 0.0, 0.0027, 0.2791, 1.5, 1.0, 100.0]),
array([31.6, 30.0, 0.0025, 0.0, 0.0027, 0.2791, 1.5, 1.0, 100.0]),
array([31.7, 30.0, 0.0025, 0.0, 0.0027, 0.2791, 1.5, 1.0, 100.0]),
array([31.8, 30.0, 0.0025, 0.0, 0.0027, 0.2791, 1.5, 1.0, 100.0]),
...
)
我的结果需要将
numpy.array
的每个元素与
元组中的所有其他元素配对。挑战在于
numpy.array
出现在元组中的任意位置,因此我无法保证索引

结果需要是
numpy.array
s的iterable(最好是
tuple
),如下所示:

(array([ 31.5,  31.6,  31.7,  31.8,  31.9,  32. ,  32.1,  32.2,  32.3,
    32.4,  32.5,  32.6,  32.7,  32.8,  32.9,  33. ,  33.1,  33.2,
    33.3,  33.4,  33.5,  33.6,  33.7,  33.8,  33.9,  34. ,  34.1,
    34.2,  34.3,  34.4,  34.5,  34.6,  34.7,  34.8,  34.9,  35. ,
    35.1,  35.2]), 30.0, 0.0025, 0.0, 0.0027, 0.2791, 1.5, 1.0, 100.0)
(array([31.5, 30.0, 0.0025, 0.0, 0.0027, 0.2791, 1.5, 1.0, 100.0]),
array([31.6, 30.0, 0.0025, 0.0, 0.0027, 0.2791, 1.5, 1.0, 100.0]),
array([31.7, 30.0, 0.0025, 0.0, 0.0027, 0.2791, 1.5, 1.0, 100.0]),
array([31.8, 30.0, 0.0025, 0.0, 0.0027, 0.2791, 1.5, 1.0, 100.0]),
...
)

我尝试过提出的解决方案和
itertools.product
。SE解决方案假定有两个独立的阵列,并且
itertools。产品
也不是正确的解决方案。

如果您不知道
阵列的位置,您只需找到它即可。我只想将其编码如下:

from numpy import array, ndarray

a = (array([ 31.5,  31.6,  31.7,  31.8,  31.9,  32. ,  32.1,  32.2, 32.3,
    32.4,  32.5,  32.6,  32.7,  32.8,  32.9,  33. ,  33.1,  33.2,
    33.3,  33.4,  33.5,  33.6,  33.7,  33.8,  33.9,  34. ,  34.1,
    34.2,  34.3,  34.4,  34.5,  34.6,  34.7,  34.8,  34.9,  35. ,
    35.1,  35.2]), 30.0, 0.0025, 0.0, 0.0027, 0.2791, 1.5, 1.0, 100.0)

for i, aa in enumerate(a):
    if isinstance(aa, ndarray):
        break

t = tuple(s for j, s in enumerate(a) if j != i)

newlist = []
for aa in a[i]:
    newlist.append(array((aa,) + t)))
result = tuple(newlist)

如果您不知道
数组的位置
,只需找到它即可。我只想将其编码如下:

from numpy import array, ndarray

a = (array([ 31.5,  31.6,  31.7,  31.8,  31.9,  32. ,  32.1,  32.2, 32.3,
    32.4,  32.5,  32.6,  32.7,  32.8,  32.9,  33. ,  33.1,  33.2,
    33.3,  33.4,  33.5,  33.6,  33.7,  33.8,  33.9,  34. ,  34.1,
    34.2,  34.3,  34.4,  34.5,  34.6,  34.7,  34.8,  34.9,  35. ,
    35.1,  35.2]), 30.0, 0.0025, 0.0, 0.0027, 0.2791, 1.5, 1.0, 100.0)

for i, aa in enumerate(a):
    if isinstance(aa, ndarray):
        break

t = tuple(s for j, s in enumerate(a) if j != i)

newlist = []
for aa in a[i]:
    newlist.append(array((aa,) + t)))
result = tuple(newlist)

如果您确定元组只包含一个
np.Array

C = [z for z in A if type(z) is not np.ndarray]
B = np.array([np.append(y,C) for y in [np.nditer(x) for x in A if type(x) is np.ndarray][0]]) 
#B can be a tuple or a list

如果您确定元组只包含一个
np.Array

C = [z for z in A if type(z) is not np.ndarray]
B = np.array([np.append(y,C) for y in [np.nditer(x) for x in A if type(x) is np.ndarray][0]]) 
#B can be a tuple or a list

您可以这样使用itertools产品:
list(itertools.product([1,2,3],[4,5]])
=>
[(1[4,5]),(2[4,5]),(3[4,5])]
,这或多或少是您想要的。重要的部分是使第二个列表(数组/元组)成为一个单元素列表(数组/元组),如果我可以识别np.array的位置,是的,但是它返回到任意位置,这样我就无法对它进行索引,否则,是的,这会起作用。要将元组一分为二,可以使用类似于
[x对于s中的x,如果type(x)=np ndarray][0]
[x代表s中的x,如果type(x)!=np.ndarray]
,其中
s
是原始元组。什么样的库在不确定的位置返回数组?无论如何,要解决这个问题,您可以检查每个元素的类型或查看它是否具有特定属性(例如
mean
)。这属于“LookBeforeYouLeap”类别。您可以通过以下方式使用itertools产品:
列表(itertools.product([1,2,3],[4,5])
=>
[(1[4,5]),(2[4,5]),(3[4,5]),
这或多或少是您想要的。重要的部分是使第二个列表(数组/元组)成为一个元素列表(数组/元组)如果我能确定np.array的位置,是的,但它返回到任意位置,因此我无法对它进行索引,否则,是的,这会起作用。要将元组一分为二,可以使用类似于
[x for x in s If type(x)==np.ndarray][0]
[x for x in s If type(x)!=np.ndarray]
,其中
s
是原始元组。什么样的库在不确定的位置返回数组?无论如何,要解决这个问题,您可以检查每个元素的类型或查看它是否具有特定属性(例如
mean
)。这属于“LookBeforeYouLeap”类别。