0

I have a list of n ndarrays and I want to loop through the list to split them into 2 variables such that the ith array is assigned to "a" and rest of them to "b" such that b doesnt contains i. (Say when i=0, it goes to a and rest go to be, now i=1, it goes to a but rest including i=0 and excluding i=1 go to b)

#n=3
lis=[x,y,z] #where x,y,z are ndarrays
for i in lis:
a=i
b=lis.remove(i)

which gives me a value error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

So I tried the below:

b=[ j for j in lis if not (j==i).all()]

But this returns an empty list.

I used the itertools.permutations but it gave an output of 4 arrays instead of all the permutations.

At this point, I am out of ideas. I am asking for a direction so that I can get this to work. Any help would be appreciated.

sra7687
  • 193
  • 1
  • 13
  • `a, b = lis.pop(i), lis` – Sayandip Dutta Feb 08 '20 at 06:12
  • No it doesnt works with ndarrays. – sra7687 Feb 08 '20 at 06:48
  • I must be misunderstanding. but if `lis = [x, y, z]`, and `lis` is of type `list` and `x, y, and z` are `numpy.ndarray`, then if you want to store the array at `i`-th element into `a` and rest of the arrays as a `list` into `b` the above code works. – Sayandip Dutta Feb 08 '20 at 06:51
  • You get the error in `remove(i)`, since that selects the item by value, and equality tests on arrays produce arrays. If you `for i in range(len(lis):...`, you can `pop` by index. Just be careful about making necessary list copies. `pop` modifies the list itself. – hpaulj Feb 08 '20 at 08:26
  • https://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous – AMC Feb 08 '20 at 21:42

1 Answers1

0

You already have y, you can get the others by just deleting y from lis.

Maybe you could try this:

for i in range(len(lis)): if np.array_equal(lis[i],y): del lis[i] break

Devon Horizon
  • 93
  • 1
  • 4