How do I intersect each position on my list?

Asked

Viewed 280 times

2

I tried it in the following ways based on answers achieved here in the forum.

First form:

A = [[1,2,3,4,5,6,7,8,9,10],[1,3,5,7,8,9,10,20],[4,5,7,13,16,20,21,30]]
B = [1,2,3,4,5,6,7,8,9]
C = [10,11,12,13,14,15,16,17,18,19]
D = [20,21,22,23,24,25,26,27,28,29,30]

def contagem_interseccao(a,b,c,d):
    s = set(a)
    return len(s.intersection(b,c,d))

print(contagem_interseccao(A,B,C,D))

Second form:

A = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10),(1,3,5,7,8,9,10,20),(4,5,7,13,16,20,21)
B = 1, 2, 3, 4,5,6,7,8,9
C= 10,11,12,13,14,15,16,17,18,19
D=  20,21,22,23,24,25

def contagem_interseccao(a, b,c,d):
    s = set(a)
    return len(s.intersection(b,c,d)) 

print(contagem_interseccao(A, B,C,D))

Third form:

A = [[1,2,3,4,5,6,7,8,9,10],[1,3,5,7,8,9,10,20],[4,5,7,13,16,20,21,30]]
B = [1,2,3,4,5,6,7,8,9]
C = [10,11,12,13,14,15,16,17,18,19]
D = [20,21,22,23,24,25,26,27,28,29,30]

n = len(set(A) & set(B))
l = len(set(A) & set(C))
k = len(set(A) & set(D))

print(list(n,l,k))

I expected the following answer:

[9 1 0, 6 1 1, 3 2 3]

In 2 cases the error is always the same: TYPE ERROR: UNHASHABLE TYPE: "LIST"

And in 1 case the result is 0.

Is this set usage wrong? Am I on the right track? Could someone give me a light? Grateful.

1 answer

6


The point is that your object A is a list of lists. It is only possible to generate the object set from types hashables, but since the list is a changeable type, it is not hashable, as the error message states.

Imagine that you had the list [[1], [1, 2]] and could generate a set from it:

>>> l = [[1], [1, 2]]
>>> s = set(l)
>>> print(s)
{[1], [1, 2]}

What would happen if we added the number 2 to the first element of the list within the set:

>>> s[0].append(2)
>>> print(s)
{[1, 2], [1, 2]}

What should happen to the set? You have two equal elements, so one of them would be removed? Which one? This is just a hypothetical example to explain simply how the set works. It is actually a table hash who uses the hash, obviously, the object to identify it and verify duplicities. By definition, mutable types are not hashables, because with each change the value of hash would vary.

Other than that, your logic is wrong. You’re trying to compare a list of lists with a list of integers. That makes no sense. What you need to do is compare each list of the list lists, A, with the list of integers, B, C and D.

A = [[1,2,3,4,5,6,7,8,9,10], [1,3,5,7,8,9,10,20], [4,5,7,13,16,20,21,30]]
B = [1,2,3,4,5,6,7,8,9]
C = [10,11,12,13,14,15,16,17,18,19]
D = [20,21,22,23,24,25,26,27,28,29,30]

n = [len(set(a) & set(B)) for a in A]
l = [len(set(a) & set(C)) for a in A]
k = [len(set(a) & set(D)) for a in A]

print(n, l, k)

But this generates the following result:

[9, 6, 3] [1, 1, 2] [0, 1, 3]

And I realized it’s different than you expected, because I compared B, C and D to all of A’s lists on the same list. Apparently you hoped that n would be the comparison of A[0] with B, C and D, which l would be the comparison of A[1] with B, C and D, so on. So one way to do it would be:

>>> print([len(set(a) & set(i)) for a in A for i in [B, C, D]])
[9, 1, 0, 6, 1, 1, 3, 2, 3]
  • 1

    Obg by Anderson class, I mean, answer, hahaha. I had not understood at first and I spent all these days (nights) trying to solve the problem. His reply helped me in two important steps of my project and it became much easier to analyze the data. I am new (2 months) in programming and the way you answered made me have to research and learn more from it and I thank you for that tbm. Well, that’s it man, obg once again and see you on the site, abç.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.