Python中的列表问题 [英] Problem with lists in Python

查看:57
本文介绍了Python中的列表问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,我在列表排序方面遇到一个小问题。以下是关于我的问题的简要说明,



这是我要列出的清单,

 GroceryList = ['  apples'' 洋葱' 番茄''  Brinjal''  pineapples''  Potatoes''  Carrots''  Biscuts''  choclates'] 





我用GroceryList.sort()对列表进行排序。



但是我的输出已经排序了大写的方式首先排序ers然后排序下面的套装字母。这是我的输出,



 ['  Biscuts''  Brinjal''  Carrots''  洋葱'  Potatoes''  Tomatoes''  apples''  choclates',< span class =code-string>'  pineapples'] 





如何解决这个问题?



我错过了什么吗?



谢谢

解决方案

你应该试试

 GroceryList.sort(key = lambda v:v.upper())


Sir, I have a small problem in sorting of lists. Here is the brief explanation on my problems,

Here is my list which is to be sorted,

GroceryList = ['apples','Onions','Tomatoes','Brinjal','pineapples','Potatoes','Carrots','Biscuts','choclates']



I have used GroceryList.sort() to sort the list.

But my output is sorted in the way that Upper case letters are sorted first and then lower cased letters are sorted next.Here is my output,

['Biscuts', 'Brinjal', 'Carrots', 'Onions', 'Potatoes', 'Tomatoes', 'apples', 'choclates', 'pineapples']



How to solve this?

Have I missed anything?

Thank you

解决方案

You should try

GroceryList.sort(key=lambda v: v.upper())


这篇关于Python中的列表问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆