本文介绍了按字符串的长度对列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
words = ["dog", "apple", "bear"]
def len_words(words):
w1 = []
for p in words:
w1.append(len(p))
pass
for p in words:
words.sort(key=len_words)
我试图按此代码排序.但是我的结果是与给定输入相同的列表.没有排序.
I tried to sort by this code. But my result is the same list as given input. It isn't sorted.
推荐答案
这应该有效:
words = ["dog", "apple", "bear"]
words.sort(key=len)
这篇关于按字符串的长度对列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!