def duplicate_count(s):
return len([c for c in set(s.lower()) if s.lower().count(c)>1])
I'm having difficulty understanding how this code works. I was doing a codewars challenge to return the number of elements with duplicates in a string.
eg. Asasd --> 2
I came up with my own implmentation but I wasn't able to really understand what this code does. If anyone could point me in the direction, it would be much appreciated :)
This is, first of all, a highly inefficient solution to the problem. But let's break it down:
s.lower()
would convert all the characters in a string to lower case:
In [1]: s = "Hello, WORLD" In [2]: s.lower() Out[2]: 'hello, world'
set(s.lower())
would create a set (make sure to read about what sets are) of characters from a string - eliminating all the duplicates:
In [3]: set(s.lower()) Out[3]: {' ', ',', 'd', 'e', 'h', 'l', 'o', 'r', 'w'}
for c in set(s.lower())
iterates over every single character in a set we created above if s.lower().count(c)>1
. The count(c)
here would count how many times c
appears in the string. The >1
helps us to leave characters that are met more than 1 time in a string [c for c in set(s.lower()) if s.lower().count(c)>1]
is called a list comprehension . It is basically a short way of creating a list. Here, we are creating a list of characters that occur in a string more than one time. Check out this topic about how to verbalize and read the list comprehensions . len()
then just gets us the length of the list To summarize, you iterate over the unique characters in a given string and count which of them occur in a string more than one time.
set(s.lower()) # gives unique elements in lower case
and
s.lower().count(c)>1 #checks if an element shows up more than once
All in all the function finds number of not unique elements in a string ,ignoring case.
I believe using collections.Counter
is more efficient:
In [7]: from collections import Counter
In [8]: sum(v > 1 for v in Counter("Aabcc".lower()).values())
Out[8]: 2
def duplicate_count(s):
result = []
for c in set(s.lower()):
if s.lower().count(c) > 1:
result.append(c)
return len(result)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.