How do I intersect each position in my list?

1

I have tried the following ways based on the answers obtained 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 way:

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 use of the set wrong? Am I on the right path? Could someone give me a light? Thankful.

    
asked by anonymous 18.08.2018 / 08:47

1 answer

5

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

Imagine that you owned 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 in 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? This is just a hypothetical example to explain simply how the set works. It is actually a hash table that uses the hash , of course, to identify it and check for duplicates. By definition, mutable types are not hashables , because with each change the hash value would vary.

Otherwise, your logic is wrong. You are 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 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 yields the following result:

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

And I realized that it is different from what you expected, as I compared B, C, and D with all the A lists in the same list. Apparently you expected% w / o% would be the comparison of% w / o with% with B, C, and D, which% w / w would be the% w / w comparison with B, C & D, and 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]
    
18.08.2018 / 14:12