Skip to content

Instantly share code, notes, and snippets.

@sincerefly
Created June 3, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sincerefly/2163389307d7b1b6367e to your computer and use it in GitHub Desktop.
Save sincerefly/2163389307d7b1b6367e to your computer and use it in GitHub Desktop.
获取字符串中重复出现的字符个数
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