我是靠谱客的博主 含蓄小熊猫,这篇文章主要介绍python列表按照长度排序_Python程序根据元素的长度对列表进行排序?,现在分享给大家,希望可以做个参考。

在这里,我们使用一个用户输入数组,并且必须根据元素的长度对列表进行排序。在这里,我们使用Python内置函数sorted()。

示例Input::[“mona”,”pp”,”aaa”]

Lengths are [4,2,3]

So, the sorted array should be [2,3,4]

Output::[“pp”,”aaa”,”mona”]

算法Step 1: Input list element.

Step 2: apply sorted (A,len) function.

范例程式码# To sort a list

def sortedlist(A):

newlist = sorted(A, key=len)

return newlist

# Driver code

A=list()

n=int(input("Enter the size of the List ::"))

print("Enter the Element ::")

for i in range(int(n)):

k=input("")

A.append(k)

print("SORTED LIST ::>",sortedlist(A))

输出结果Enter the size of the List ::5

Enter the Element ::

mona

gulli

adwaita

aadrika

pinki

SORTED LIST ::> ['mona', 'gulli', 'pinki', 'adwaita', 'aadrika']

最后

以上就是含蓄小熊猫最近收集整理的关于python列表按照长度排序_Python程序根据元素的长度对列表进行排序?的全部内容,更多相关python列表按照长度排序_Python程序根据元素内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部