无法在这里找出逻辑?

时间:2019-08-11 16:09:48

标签: python python-3.x

好吧,我遇到了一段代码,以降序排列数字。 但是对于列表中输入的每个数字,我都会将其转换为字符串,并且输出会有所不同,而且我不知道该怎么做?

def returnString(inputListInt):
    result=''
    inputListStr=[str(x) for x in inputListInt]

    for i in range(len(inputListInt)):
        for j in range(len(inputListInt)-1):
            if (inputListStr[j]<inputListStr[j+1]):
                inputListStr[j], inputListStr[j+1] = inputListStr[j+1], inputListStr[j]
    for i in inputListStr:
        result+=i
    print(result)


if __name__ =="__main__":   
    input = [int(x) for x in input().split(',')]
    returnString(input)

所以如果输入是5,2,34,7,30,9,6 而不是输出343097652 是976534302

1 个答案:

答案 0 :(得分:2)

这是因为字符串按字母顺序排序,然后按字典顺序进行比较。

这给出了看起来很奇怪的结果:

'20' < '3' # because the character '3' comes after the character '2'
'540' < '60' # because '6' comes after '5'

同样,'ForceBru' > 'FenceBru'是因为'o' > 'e'之后的'o''e'之后。

相关问题