Python Code:检查重复(isDuplicate)
本文列出的代码为:
在Python中,如何检查某段字符串中是否有重复的字符(包括空格),重复程度为多少?
具体代码如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#coding=utf-8 print "-----------------" print "检查目标字符串是否有重复的字符(包含空格)" print "-----------------" target_string = raw_input("Please input Target String:\n") print "Target is:",target_string unique_char = [] for loop_item in target_string: if (unique_char.count(loop_item) == 0): unique_char.append(loop_item) for loop_list in unique_char: item_count = target_string.count(loop_list) if (item_count > 1): print "Duplicate char is:",loop_list," Duplicate Count is:",item_count print "-----------------" print "End,..." |
执行效果如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
D:\python_data>dir 驱动器 D 中的卷是 Data Logic 卷的序列号是 E4C7-58A1 D:\python_data 的目录 2015/03/13 16:57 <DIR> . 2015/03/13 16:57 <DIR> .. 2015/03/13 16:57 602 isDuplicate.py 2015/03/13 16:57 0 isDuplicate.py.bak 2 个文件 602 字节 2 个目录 237,557,219,328 可用字节 D:\python_data>c:\Python27\python.exe isDuplicate.py ----------------- 检查目标字符串是否有重复的字符(包含空格) ----------------- Please input Target String: angela Baby Target is: angela Baby Duplicate char is: a Duplicate Count is: 3 ----------------- End,... D:\python_data>c:\Python27\python.exe isDuplicate.py ----------------- 检查目标字符串是否有重复的字符(包含空格) ----------------- Please input Target String: Hello world, adamhuan. Target is: Hello world, adamhuan. Duplicate char is: l Duplicate Count is: 3 Duplicate char is: o Duplicate Count is: 2 Duplicate char is: Duplicate Count is: 2 Duplicate char is: d Duplicate Count is: 2 Duplicate char is: a Duplicate Count is: 3 ----------------- End,... D:\python_data> |
————————————————————————
Ending。