Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок0
From given three array find the duplicates which have same attributes in all the three arrays.
It runs fine but it prints 1 more if more than one duplicate is present. https://code.sololearn.com/cq6PI2W1T5bP/?ref=app
5 ответов
+ 2
print(len(l1)-len(set([tuple(i) for i in l1])))
+ 1
Thanks it work. But can you explain that?
+ 1
Ok thanks. Why should I convert it to tuple. Why It shows error when I use len(set(l1))
+ 1
In simple terms, lists can't be used inside sets, but tuples can.
0
[tuple(i) for i in l1] converts every element of l1 into a tuple.
[[1, 2], [3, 4]] -> [(1, 2), (3, 4)]
set(...) removes all duplicates (it can do much more than that).
len(l1)-len(set(...)) returns the total number of items minus the number of unique items, resulting in the number of duplicate items.