# coding:utf-8 def find_longest_no_repeat_substr(one_str): ''''' 找出来一个字符串中最长不重复子串 ''' res_list = [] length = len(one_str) for i in range(length):#分别以字符串的每个字符作为自串的第一个字符 tmp = one_str[i]#记录不重复自串的中间变量 for j in range(i + 1, length): if one_str[j] not in tmp: tmp += one_str[j] else: break res_list.append(tmp) #保存所有的不重复子串 res_list.sort(lambda x, y: cmp(len(x), len(y)),reverse = True)#根据每个不重复子串长度降序排列 return res_list[0] if __name__ == '__main__': one_str_list = ['120135435', 'abdfkjkgdok', '123456780423349'] for one_str in one_str_list: res = find_longest_no_repeat_substr(one_str) print '{0}最长非重复子串为:{1}'.format(one_str, res)