python - Sorted如何判定一个单词是否是按序排列?
阿神
阿神 2017-04-17 17:35:36
0
1
583

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

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']
阿神
阿神

闭关修行中......

membalas semua(1)
迷茫
def sorted_words(l):
    return list(filter(lambda x: x == ''.join(sorted(x)), l))
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan