Created
June 3, 2015 14:10
-
-
Save sincerefly/2163389307d7b1b6367e to your computer and use it in GitHub Desktop.
获取字符串中重复出现的字符个数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def duplicate_count(s): | |
return len([c for c in set(s.lower()) if s.lower().count(c)>1]) | |
#test.assert_equals(duplicate_count("abcde"), 0) | |
#test.assert_equals(duplicate_count(""), 0) | |
#test.assert_equals(duplicate_count("abcdea"), 1) | |
#test.assert_equals(duplicate_count("indivisibility"), 1) | |
#test.assert_equals(duplicate_count("aabbcde"), 2) | |
#test.assert_equals(duplicate_count("aabbcdeB"), 2) | |
#test.assert_equals(duplicate_count("Indivisibilities"), 2) | |
#test.assert_equals(duplicate_count("aabbcdeBBBBBBBBB"), 2) 测试示忽略了这个,如下: | |
def duplicate_count(text): | |
c = 0 | |
for i in list(set(text)): | |
if text.count(i) > 1: | |
c = c + 1 | |
return c | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment