Python俱乐部
Python
小课题
京东优惠券
给定一个字符串,找出里面出现次数最多的字符。
def find_most_char(s): l = [c for c in s] l.sort() s = "".join(l) print s l = [s[i-1]==s[i] and s[i] or "," + s[i] for i in range(len(s))] s = "".join(l) l = s.split(',') l.sort(lambda x,y: cmp(len(x), len(y))) print l[-1], len(l[-1]) if __name__ == "__main__": s = "&(&$#(JFLSDJFOIWEfdjsklfewosd2378907345asdsdwsdqs" find_most_char(s)
输出结果:
#$&&((0233457789DEFFIJJLOSWadddddeffjkloqssssssww ssssss 6