gpt4 book ai didi

python - 在 Python 中,为什么 [2] 小于 (1,)?

转载 作者:IT老高 更新时间:2023-10-28 20:50:14 25 4
gpt4 key购买 nike

背景

我一直在尝试找出为什么我的 AI 会做出一些疯狂的举动,并且我将问题追溯到使用 Python 2.7.2 时的以下行为

>>> print [2]>[1]
True
>>> print (2,)>(1,)
True
>>> print [2]>(1,)
False (WHY?)

>>> print [2]<[1]
False
>>> print (2,)<(1,)
False
>>> print [2]<(1,)
True (WHY?)

似乎列表总是小于元组。

这不是我对 documentation 的期望

Tuples and lists are compared lexicographically using comparison of corresponding elements. This means that to compare equal, each element must compare equal and the two sequences must be of the same type and have the same length.

If not equal, the sequences are ordered the same as their first differing elements. For example, cmp([1,2,x], [1,2,y]) returns the same as cmp(x,y). If the corresponding element does not exist, the shorter sequence is ordered first (for example, [1,2] < [1,2,3]).

问题

这里发生了什么?我是否允许比较元组和列表 - 或者我是否只允许比较同一类型与自身?

最佳答案

比较时不强制序列,因此比较它们的类型名称。

>>> 'list' < 'tuple'
True

关于python - 在 Python 中,为什么 [2] 小于 (1,)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17371854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com