简体   繁体   English

如何在 python 中按长度对字符串的二维列表进行排序?

[英]How to sort a 2D list of strings by length in python?

I have a 2D list in python which contains first and last names of different people.我在 python 中有一个二维列表,其中包含不同人的名字和姓氏。 It looks like this: teachers=[["Rachelle", "Goodyear"],["Mike","Anderson"], ["Jeremy","Smith"]] And I want to sort them by the length of their last name.看起来像这样: teachers=[["Rachelle", "Goodyear"],["Mike","Anderson"], ["Jeremy","Smith"]]我想按他们最后的长度对它们进行排序姓名。 I've tried different solutions but it always sort by their first name... Please help me.我尝试了不同的解决方案,但它总是按他们的名字排序......请帮助我。 Thank you.谢谢你。

Here is a possible solution:这是一个可能的解决方案:

sorted_teachers = sorted(teachers, key=lambda x: len(x[1]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM