Python Can';你不明白这里的逻辑吗?
嗯,我遇到了一段按降序排列数字的代码。 但对于列表中输入的每一个数字,我都将其转换为字符串,输出会发生变化,我不知道如何进行Python Can';你不明白这里的逻辑吗?,python,python-3.x,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):
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)
def returnString(inputListInt):
结果=“”
inputListStr=[inputListInt中x的str(x)值]
对于范围内的i(len(inputListInt)):
对于范围内的j(len(inputListInt)-1):
如果(inputListStr[j],这是因为字符串按字母顺序排序,并按字典顺序进行比较
这会产生奇怪的结果:
'20' < '3' # because the character '3' comes after the character '2'
'540' < '60' # because '6' comes after '5'
'20'<'3'#因为字符'3'在字符'2'之后
'540'<'60'#因为'6'在'5'之后
同样地,'ForceBru'>'FenceBru'
因为'o'>'e'
因为'o'
在'e'
之后,因为'9'
'34'
。数字比较,但字符串比较的是字典形式的。'34'
因为'5'
的原因与'BA相同
谢谢austin macha!因此在'34'和'30'中,它首先将'3'与所有其他值进行比较,如果它们相等,则移动到下一个字符?@joel,它将比较'3'与'3'
,但它们相等,因此它将移动到'4'与'0'
。后者较小,因此'34'>'30'
。