Python 3.x 为什么可以';Python不理解单词形式的整数吗?

Python 3.x 为什么可以';Python不理解单词形式的整数吗?,python-3.x,numbers,integer,logic,Python 3.x,Numbers,Integer,Logic,我在任何地方都找不到问题的答案,而且我对Python还是相当陌生的。这个问题的目的主要是了解Python是如何工作的以及它的局限性。 提供一个模块,用于将数字从整数转换为整数的字形式。但是,如果我想运行这样的代码,没有任何模块以与链接中的模块相反的方式工作 a = "five" b = 2 if b < a: print("a is higher than b") a=“五” b=2 如果b来演示一个假设的shell 对于如何隐式解析,有几种可能的方案: 3 < "two"

我在任何地方都找不到问题的答案,而且我对Python还是相当陌生的。这个问题的目的主要是了解Python是如何工作的以及它的局限性。 提供一个模块,用于将数字从整数转换为整数的字形式。但是,如果我想运行这样的代码,没有任何模块以与链接中的模块相反的方式工作

a = "five"
b = 2
if b < a:
    print("a is higher than b")
a=“五”
b=2
如果b
我收到
类型错误:无序类型:int()


那么,为什么Python不能将这个字符串识别为单词形式中的数字呢?是不是类似于“Python没有被构建来识别数字的单词形式”?

注:我用
>
来演示实际的Python代码,用
?->
来演示一个假设的shell

对于如何隐式解析,有几种可能的方案:

3 < "two"
另一种方法是将数字转换为等效的字串,然后按字母顺序进行比较:

 >>> "three" < "two"
 True
或者假设您试图将两个字符串转换为数字,并将它们作为数字进行比较,那么排序字符串将中断:

?-> words = "please give me five apples".split()
?-> words.sort()
?-> words
['five', 'apples', 'give', 'me', 'please']

因此,从任何角度看,隐式添加此功能都会彻底破坏许多其他完美的功能。
编辑

我很好奇排序实际上是如何工作的,所以我创建了一个类来进行这种比较:

from functools import total_ordering

@total_ordering
class number_word:
    key = dict(enumerate(
               ("zero one two three four five six seven eight nine ten".split())
              ))
    key.update({v:k for k,v in key.items()})

    def __init__(self,value):
        alt = self.key.get(value,None)
        if isinstance(value,str):
            self.word = value
            self.num = alt

        elif isinstance(value,int):
            self.num = value
            self.word = alt
        else:
            raise TypeError("must be str or int")

    def __repr__(self):
        return "nw(%r)"%self.word

    def __eq__(self,other):
        if not isinstance(other,number_word):
            other = word_number(other)

        if self.num == None == other.num:
            #neither are valid numbers, compare as strings
            return self.word == other.word
        else:
            return self.num == other.num

    def __lt__(self,other):
        if not isinstance(other,number_word):
            other = word_number(other)

        if self.num is None or other.num is None:
            return self.word < other.word
        else:
            return self.num < other.num
理论上,这三者应该是相同的,尤其是
num_sort==backward[::-1]
,但结果如下:

['five', 'from', 'goes', 'one', 'range(1,6)', 'to']
['from', 'goes', 'one', 'five', 'range(1,6)', 'to']
['one', 'five', 'from', 'goes', 'range(1,6)', 'to']
因此,是的,它确实打破了字符串排序。

是的:“Python还没有被构建来识别数字的单词形式”:特殊情况不足以打破规则。面对模棱两可的情况,拒绝猜测的诱惑。应该有一个——最好只有一个——显而易见的方法来做到这一点。显式比隐式好。
?-> words = "please give me five apples".split()
?-> words.sort()
?-> words
['five', 'apples', 'give', 'me', 'please']
from functools import total_ordering

@total_ordering
class number_word:
    key = dict(enumerate(
               ("zero one two three four five six seven eight nine ten".split())
              ))
    key.update({v:k for k,v in key.items()})

    def __init__(self,value):
        alt = self.key.get(value,None)
        if isinstance(value,str):
            self.word = value
            self.num = alt

        elif isinstance(value,int):
            self.num = value
            self.word = alt
        else:
            raise TypeError("must be str or int")

    def __repr__(self):
        return "nw(%r)"%self.word

    def __eq__(self,other):
        if not isinstance(other,number_word):
            other = word_number(other)

        if self.num == None == other.num:
            #neither are valid numbers, compare as strings
            return self.word == other.word
        else:
            return self.num == other.num

    def __lt__(self,other):
        if not isinstance(other,number_word):
            other = word_number(other)

        if self.num is None or other.num is None:
            return self.word < other.word
        else:
            return self.num < other.num
words = "range(1,6) goes from  one to five".split()
correct = sorted(words)
num_sort = sorted(words,key=number_word)
backward = sorted(words,key=number_word, reverse=True)

print(correct)
print(num_sort)
print(backward[::-1])
['five', 'from', 'goes', 'one', 'range(1,6)', 'to']
['from', 'goes', 'one', 'five', 'range(1,6)', 'to']
['one', 'five', 'from', 'goes', 'range(1,6)', 'to']