Python 列表大于数字 [英] Python list greater than number

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

问题描述

我发现列表大于数字.

<预><代码>>>>[1,2,3] >1000真的

这有什么原因吗?我无法使用 int([1,2,3]) 将列表转换为 int.int 不能转换为带有 list(1000) 的列表.那么python是如何比较两者的呢?

解决方案

在这种不匹配"类型的情况下,类型按类型名称按字典顺序列出:列表"按字母顺序排在int"之后,因此它更大.

<块引用>

CPython 实现细节:不同类型的对象,除了数字按其类型名称排序;相同类型的对象不支持正确比较的按地址排序.(来源)

排序没有语言规范(除了它是一致的).碰巧 CPython 是最常见的实现,其中存在按类型名称按字典顺序排序的语言细节.

I have discovered that a list is greater than a number.

>>> [1,2,3] > 1000
True

Is there some reason why this works? I can't convert a list to an int with int([1,2,3]). The int can't be converted to a list with list(1000). So how is python comparing the two?

解决方案

In this case of "mismatched" types, the types are listed lexicographically by type name: a "list" comes after an "int" in alphabetical ordering, so it is greater.

CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address. (source)

There is no language specification for the ordering (apart from the fact that it is consistent). It just happens to be the case that CPython is the most common implementation in which there is this language detail of being ordered lexicographically by type names.

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

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