0

I want to get those values of new_pattern_dataset which are not present in all_pattern_dataset. I am writing the following code:

new_pattern_dataset=[x for x in new_pattern_dataset if x not in all_pattern_dataset]

where

print(type(new_pattern_dataset))
new_pattern_dataset
OUTPUT:
[(1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0),
 (0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1),
.
.
.
(1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0),
 ...]
print(type(all_pattern_dataset))
all_pattern_dataset
OUTPUT:
<class 'list'>
[array([0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1]),
 array([0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1]),
.
.
.
array([0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0]),
 ...]

This gives me the error: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() Can somebody please explain what am I doing wrong and how to correct it?

Also, since the type of both new_pattern_dataset and all_pattern_dataset is 'list', why do they have different formats?

Gavin Wong
  • 1,254
  • 1
  • 6
  • 15
  • Could you share please example of input and the expected output? – Gabio Jul 12 '20 at 13:36
  • Also, it would help if you showed the code that generates the error message, preferably in a [mcve]. – Dennis Sparrow Jul 12 '20 at 14:29
  • Does this answer your question? [ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()](https://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous) – Trenton McKinney Aug 11 '20 at 18:05

1 Answers1

0

Comparing a list with another list can be tricky since python doesn't know if u want to compare each element in the list/sublist with the element of another list or the lists themself.

Therefore you get the error: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() You can fix this by handling the sublist like strings:

new_pattern_dataset = [str(x) for x in new_pattern_dataset if str(x) not in [str(list(y)) for y in all_pattern_dataset]]

Another way to get the difference is to use sets, because in opposite to lists, sets allow natively to check for differences between 2 sets:

new_pattern_dataset = list(set(str(x) for x in new_pattern_dataset) - set([str(list(y)) for y in all_pattern_dataset]]))
Andreas
  • 8,694
  • 3
  • 14
  • 38