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']
阅读 3.6k
1 个回答
def sorted_words(l):
    return list(filter(lambda x: x == ''.join(sorted(x)), l))
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题