Python List边界问题!,怎么修改?并且有什么方法能够避免!一不小心就掉坑啊!

x='asasdaessaasccvasadcda'
l=[y for y in x]
for i in range(len(l)):
    if l[i]>l[i+1]:
        l.insert(i+1,' ')#我应该是超出边界了!!!
content = ''.join(l)
l2=content.split()
print l2
l3=[]
for n in l2:
    a=len(n)
    l3.append(a)
print 'Longest substring in alphabetical order is:%s'%(l2[l3.index(max(l3))])

这是个求按照英文字母顺序排序,连续最长的找出来,上面是我写的,很屎。。。。。。就想问大大们,问题怎么修改?我应该是超出边界了!!!

asdhjhg
浏览 2089回答 1
1回答

慕瓜9220888

MNWUDPFPLIGIXYIBKASXTSBSCQBFMRQZJGXSTIKTCSJAANCLDMWNEBIQZIRXUBKSXFONOFVFINMVULLYJKOXQJWXJZYQYHAXOYKNIAZBKKXBUVXVKGSLLFNWYDMFMMTNSECJZSBKBDSKXFLBRPSHABYEFLNGPODGDUUSBKDQAVBLUMLBCSBBQBKDWCZKKWEWVLJQHYOCVUJAGDBCANSRIPPYIQIVKHKVVTIKJCSTGDVBKJIPMXHBZYHGJGNEBJYFIPPMXPKKAPGAKOIMVWVIFPWVDMFEVLSCTXKMXVSJTTXQZEPJPYWEAJLMGGJFFVKWDAHRGYNGPCZZCCKTMVLPZQJUDTVWGGXFNCCUEYPRGFUTSRVFGKZYHANDUBTSKTUOSSDETCUTGUKBFHZZYIPVCESYAAGNCMQENGFSCZAYMVEXWGTKZNGPELSPPSKTMLCITDAIKJSTGRBSHZYHYXXHBZEHTUXXOFNZIHOAHRIGPKJDXHIBZYRJXURREDFONUXSMPOZVGJTZCIDVKFMOLVMXMNMPMVOVDMDWTKXOLNWLHXYIZWENMVBROFYWVOHREBMGYHGSZCSZUZHHGGQRHZNDFYVCTFWRQFHMTAHFUTFEVMSMWHPRQXKXOBZLYNKRYOUEOXGNURRNBKDVSGMNKZBUBISTNACVUJTHHVEIBUOPCCTSYAQZZEBMVDCVRRIFPXFULUBSPFSXGZLMTZGCEAYAXOISKIHPHSMJZOGYXPZWMDHPEKCZNRLUSLUQARPQGUNFLSJZJFMPFYVLWGLDNWOLMWGQMCBKRRIIWBDSIOFWQEMYXAPFCWCHDWESPHIBDCEHEYISMADFLROZHYWVAZPCMGCHZPRUIFZRVUKMQSFFPASTPPCZALNWNWWGXLLMUQOHHENABGSKGPZCXYQYNGAJZHVONWNUKEKBETSZQNKIATDJJKKUJJSNGQNHNRGBJPGWTDBSYOGFIPFMUZRXHXDDUOMVANWHUEZEMJZZCCDYJIEXANXEMDMEVISDJBQZOVMCTOGXQPCTAAPEGPEFIBBCKTJYFFGGXFOWCSJTNGKJCVFWWGSKJMXNEYIPHNMFZMCDGSBRGWUAUUIXNFSZGQKAAZRSZPZRPUTLCTJUCQJILWLVGYZYHXNALTHGVENXHLVYPPXXUKBBCSRHWZAKROCBKDTNEWNCKNWNQHHCRBRUBIZJTXJBWZNUAHIJYABRRKSCVUWNXOOAHGHWZIAUPBJVXQTMZHPHSORKGTYZRTOXCMIGHQFNIOBXBSQKCZIREQMINZVLOLCTIQOIRENIMOPVHKAXALKSEKIIOLCLFUNZTCBGOQVOXKUHRNVQWZTZDHWOGLLEOZUPBCCLYYRLXCHOUGKUCXZLHMIVLVNWOBXUTJNWLWWTZFJKMUDPFYFBBDGVCZPGKIBIYICMGLJSBNGNNXBPONCTNADHVODGWTAAMNYNWXHYISOAMLCTDVYGPODUKRYTREQPMTKUXJBKJAJJDYENPVIVMWSKMLKUIFFOSQJIIPWWNVENMFVGAOMVOHEEVFKPIQZDTAUMSRAZMTQHAZINCDDUGIMMEDOICIVDMLEBSZPWBZBMZWMGQSLNWDAHQXVHSBBYFWBWFHMJPWX

孤独的小猪

你的代码算出来的结果好像是错误的,可以按照下面代码算出正确结果:s = "asasdaessaasccvasadcda" ls = "" for i in range(0, len(s)-1):     b = ""     ss = ""     j = 2     while j < len(s):         ss = s[i:i+j]         b = sorted(ss)         str1 = ''.join(b)         j += 1         if str1 == ss:             ks = ss         else:             break     if len(ks) > len(ls):         ls = ks print("The Longest substring in alphabetical order is "+ls)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python