0

I have some Multidimensional array like these:

a=[[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 1],[1, 1],[1, 1],[1, 5],[5, 12],[12, 6]]
b=[[0, 0],[0, 0],[0, 0],[0, 0],[0, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 5],[5, 12],[12, 6]]

I'd like to get their intersection in python . so I code c=[v for v in a if v in b]

I want [[0, 0],[0, 0],[0, 0],[0, 0],[0, 1],[1, 1],[1, 1],[1, 5],[5, 12],[12, 6]] but the code got wrong answer.

4
  • Can you explain a bit better? Describe in detail how you want doublets handled. Commented Jul 11, 2017 at 12:13
  • set(a).intersection(b) and list(set(a) & set(b)) doesn't work Commented Jul 11, 2017 at 12:18
  • I 'd like the answer is [[0, 0],[0, 0],[0, 0],[0, 0],[0, 1],[1, 1],[1, 1],[1, 5],[5, 12],[12, 6]] Commented Jul 11, 2017 at 12:21
  • Yes, but why is [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 1], [1, 1], [1, 1], [1, 5], [5, 12], [12, 6]] wrong? Commented Jul 11, 2017 at 12:22

2 Answers 2

1

You can reach if you remove from b an element that you already iterated, that way you append to res the minimum intersections of the same elements between a and b.

a=[[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 1],[1, 1],[1, 1],[1, 5],[5, 12],[12, 6]]
b=[[0, 0],[0, 0],[0, 0],[0, 0],[0, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 5],[5, 12],[12, 6]]

res = []
for _a in a:
  if _a in b:
    res.append(_a)
    b.remove(_a)

print (res)

>>> [[0, 0], [0, 0], [0, 0], [0, 0], [0, 1], [1, 1], [1, 1], [1, 5], [5, 12], [12, 6]]

EDIT:

You can iterate through a and get the number of the element occurrences, do the same for b and extend the res list with the minimum between occurence_count_a and occurence_count_b

Don't forget to check at the top whether the element is already in res list than, you don't need to extend it again as each iteration you can extend few elements to res (depends on the intersection between a and b) so when you get to a value that you iterated before, you don't need to worry about him as you extended res before with the minimum intersection of this element.

a=[[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 1],[1, 1],[1, 1],[1, 5],[5, 12],[12, 6]]
b=[[0, 0],[0, 0],[0, 0],[0, 0],[0, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 5],[5, 12],[12, 6]]

res = []
for _a in a:
  if _a not in res:
    occurence_count_a = a.count(_a)
    occurence_count_b = b.count(_a)
    res.extend ([_a for _ in range(min(occurence_count_a, occurence_count_b))])


print (res)

>>> [[0, 0], [0, 0], [0, 0], [0, 0], [0, 1], [1, 1], [1, 1], [1, 5], [5, 12], [12, 6]]
Sign up to request clarification or add additional context in comments.

2 Comments

ye,that's great .but i don't like remove,because it could make my data dirty.could u use anther function to help me.thx
@Ming , I've added an EDIT section to the answer.
0

The problem with your code was that it was only checking whether an element of list a existed in list b regardless of where exactly or how many times. Try this:

a=[[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 0],[0, 1],[1, 1],[1, 1],[1, 5],[5, 12],[12, 6]]
b=[[0, 0],[0, 0],[0, 0],[0, 0],[0, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 1],[1, 5],[5, 12],[12, 6]]

from collections import Counter as cC

ac = cC([tuple(x) for x in a])
bc = cC([tuple(x) for x in b])
print(ac)
print(bc)
c = sorted([list(x) for x, v in (ac & bc).items() for _ in range(v)])
print(c)  # [[0, 0], [0, 0], [0, 0], [0, 0], [0, 1], [1, 1], [1, 1], [1, 5], [5, 12], [12, 6]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.