0
Как найти совпадения в списках?
Есть 2 списка: A=['x' , 1 , 'e'] S=['spam' , '1' , 'w'] Как выяснить, есть ли совпадения и как их посчитать?
8 Answers
+ 3
A=['x' , '1', 'e', 'solo']
S=['spam', 'solo', '1' , 'w']
# needs to convert list to set because this intersection(&) is set operation
A=set(A)
S=set(S)
'''
Suppose,
A = {1,2,7,5,9}
B = {2,4,9}
print(A & B) => output {2,9} elements present both in the set A and set B.
& means intersection
'''
res = A & S
print(res)
print('Length', len(res))
+ 1
Спасибо
0
А что, если нужно найти совпадения в 1 списке:
A=['A' , '1' , 'w' , 'z' , '1']
Как поступить в таком случае?
0
A=['A', '1', 'w', 'z', '1']
d = {a:0 for a in A}
for a in A:
if a in d:
d[a] += 1
print(d)
0
Hm......
0
That's not exactly what I meant. This method is not suitable for further work
0
I do not deny its correctness.
0
Suppose there is a task:
In the basket there are pieces of paper with inscriptions: Kolya, Petya, Dima, Sasha
There is a list of names:Sasha, John, Alexey, Pavel.
Write a program that calculates how many boys can get a piece of paper with their name on it.