为什么Python排序将大写字母放在首位? [英] Why does Python sort put upper case items first?

查看:118
本文介绍了为什么Python排序将大写字母放在首位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不寻找解决方法.希望了解Python为什么以这种方式排序.

Not looking for a work around. Looking to understand why Python sorts this way.

>>> a = ['aaa','Bbb']
>>> a.sort()
>>> print(a)
['Bbb', 'aaa']

>>> a = ['aaa','bbb']
>>> a.sort()
>>> print(a)
['aaa', 'bbb']

推荐答案

这是因为大写char的ASCII值小于小写的ASCII值.因此,如果我们按升序对它们进行排序,则大写字母将排在小写字母

This is because upper case chars have an ASCII value lower than that of lower case. And hence if we sort them in increasing order, the upper case will come before the lower case

  • A的ASCII是65
  • a的ASCII是97
  • ASCII of A is 65
  • ASCII of a is 97

65< 97

65<97

A < a,因此,如果您按升序排序

And hence A < a if you sort in increasing order

这篇关于为什么Python排序将大写字母放在首位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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