1

I have a tuple which contains a numpy.array of arbitrary length along with scalars. Something like this:

(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)

My result needs to pair each element of the numpy.array with all the other elements in the tuple. Challenge is that the numpy.array appears in an arbitrary location within the tuple such that I cannot index with a guarantee.

The result needs to be an iterable (preferably a tuple) of numpy.arrays, something like this:

(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]),
...
)

I have tried solutions presented here and here as well as itertools.product. The SE solutions assume two independent arrays and itertools.product is not the right solution either.

Community
  • 1
  • 1
Jason Strimpel
  • 14,670
  • 21
  • 76
  • 106
  • You can use itertools product this way: `list(itertools.product([1,2,3], [[4, 5]]))` => `[(1, [4, 5]), (2, [4, 5]), (3, [4, 5])]` which is more or less what you want. The important part is making the second list (array/tuple) a one element list (array/tuple) – Pyetras Mar 18 '15 at 12:51
  • If I could identify the location of the np.array, yes, but it is returned in arbitrary locations such that I cannot index it but otherwise, yes that would work. – Jason Strimpel Mar 18 '15 at 12:53
  • to split the tuple in two you can use something like `[x for x in s if type(x) == np.ndarray][0]` and `[x for x in s if type(x) != np.ndarray]`, where `s` is the original tuple. – matiasg Mar 18 '15 at 12:53
  • 1
    What kind of library returns an array at an indetermined location? Anyway, to solve that you could check the type of each element or see whether it has a certain attribute (such as `mean`). This falls under the "LookBeforeYouLeap" category. – Oliver W. Mar 18 '15 at 12:55

3 Answers3

2

If you don't know the position of the array, you'll just have to find it. I would simply code it as follows:

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)
1
import numpy as np
x = (np.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)


components = sorted(x, key=lambda xi: np.isscalar(xi))
prefixes = components[0]
suffix = components[1:]
result = tuple(np.array([xi]+suffix) for xi in x)
Alan
  • 9,410
  • 15
  • 20
1

This is another way to do it if you are certain that your tuple contains only one 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
igavriil
  • 1,001
  • 2
  • 12
  • 18