I have two lists A = [[1, 2, 3, 4], [5, 6, 7, 8]]
and another list B = [[1, 2, 3, 4], [8, 6, 3, 2]]
. Now I want to compare the sub-strings in A
and B
. If they contain same sub-lists then I want to append them to a separate list. I did in a normal way to compare strings. But as they contain 4 values in a single item it is throwing error : The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() . I cannot find any way to solve this. Expected output from the given example is [[1, 2, 3, 4]]
-
What substring? There are no strings in your example.Note, your error message seems to imply that you are working with `numpy.ndarray` objects rather than python lists. Please provide a [mcve] with clear inputs with your expected outputs and what you've tried that is creating that error, including the full error message and stack trace/ – juanpa.arrivillaga Jul 14 '20 at 10:27
1 Answers
You can just use a normal equality to compare to sub-lists, and zip the two lists together to compare items at the same index e.g.
[a for a, b in zip(A, B) if a == b]
will return a list of all the elements which are identical in A
and B
(and at the same index in both).
I have two lists A = [[1, 2, 3, 4], [5, 6, 7, 8]] and another list B = [[1, 2, 3, 4], [8, 6, 3, 2]]. Now I want to compare the sub-strings in A and B.
You've got lists of lists, there are no substrings here.
I did in a normal way to compare strings.
What are you talking about? What's the "normal way to compare strings"?
But as they contain 4 values in a single item it is throwing error : The truth value of an array with more than one element is ambiguous.
What error? And no it is not. The truth value of an array in python is that an empty array is falsy and a non-empty array is truthy. The comparison of two arrays, meanwhile, is simply lexicographic. No ambiguity anywhere there.
Use a.any() or a.all() .
How's that even related to anything else?
I cannot find any way to solve this.
What have you actually tried, what's your reasoning behind these attempts, and what was the result?
-
Hi, not sub-strings they are sub-lists. It's my mistake. I just want to compare them. I'm new to coding. I don't know much about python also. Actually the four co-ordinates in the question are bounding box co-ordinates obtained from Template matching. I just want to compare one set with another, and if any similar ones are found then I want to add to a new list. – Durga Rao Jul 14 '20 at 10:45
-
1You can just use a normal equality to compare to sub-lists, and zip the two lists together to compare items at the same index e.g. `[a for a, b in zip(A, B) if a == b]` will return a list of all the elements which are identical in A and B (and at the same index in both). – Masklinn Jul 14 '20 at 11:28
-
"The truth value of an array in python is that an empty array is falsy and a non-empty array is truthy" you are thinking of lists. The OP is almost certainly working with `numpy.ndarray` objects. Don't answer questions which dont provide an adequate problem description and [mcve] – juanpa.arrivillaga Jul 14 '20 at 19:35