搜尋

首頁  >  問答  >  主體

python - Sorted如何判定一个单词是否是按序排列?

这是题目要求要写出来并且最终的运行结果

sorted_words(["bet", "abacus", "act", "celebration", "door"])
['act', 'bet', 'door']
>>> sorted_words(['apples', 'bananas', 'spam'])
[]
>>> sorted_words(["aims", "Zip"])
['Zip', 'aims']

它的意思不仅仅是要按字母顺序来排列单词,还需要删除非字母顺序排列的单词,并最终RUN出来

这是我根据题意写出来的一段代码 可是return出来的数值却只能按序排列,删除不了非字母排序的单词 望大神指教(字母排序 意味着如果例子是ace, a,c,e是按照字母表顺序排的,若是eat,e在a后,不符合字母表的顺序,在本题中则要删除)

def sorted_words(wordlist):
    if sorted(wordlist):
        for a in wordlist:
            if not sorted(a):
                return a.remove
    return sorted(wordlist)
    
    而我本人的答案运行后确是这样

>>> sorted_words(["bet", "abacus", "act", "celebration", "door"])
['abacus', 'act', 'bet', 'celebration', 'door']
阿神阿神2889 天前589

全部回覆(1)我來回復

  • 迷茫

    迷茫2017-04-17 17:37:36

    雷雷

    回覆
    0
  • 取消回覆