我在python的排序功能有错误的结果 [英] I have wrong results with sorting function at python

查看:45
本文介绍了我在python的排序功能有错误的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在学习 Python,希望有人能帮助我.我尝试使用 sort() 对特定列表进行升序排序,但是当我尝试这个示例时,它的输出是错误的.

Currently I am learning Python, and I hope someone can kindly help me. I tried to use sort() to sort a specific list ascending, but when I try this example its outputs is wrong.

Number_list[9,5,13]
Number_list.sort()
print Number_list

我得到的结果是:['13', '5', '9']

它应该是:['5', '9', '13']

我正在尝试编写显示最大奇数的程序,我编写的代码如下:

I am trying to write program that show the maximum odd number, and i write the code as following:

Number_list = []
print "Enter first number:"
Number_list.append (raw_input());
print "Enter second number:"
Number_list.append (raw_input());
print "Enter third number:"
Number_list.append (raw_input());
Number_list.sort()
if int(Number_list[2]) % 2 != 0 :
     print Number_list[2]
elif int(Number_list[1]) %2 != 0:
     print Number_list[1]
elif int(Number_list[0]) % 2 != 0:
     print Number_list[0]
else:
    print "There is no Odd number"

推荐答案

看起来您的列表是字符串列表而不是数字列表.你的列表是这样定义的吗?

Looks like your list is a list of strings and not a list of numbers. Is your list defined like this?

NumberList = ['9', '5', '13']

<小时>

更新:

从您更新的代码片段中,我们可以看到您如何填写列表:

From your updated code snippet we see how you fill your list:

Number_list.append(raw_input())

您必须知道,在 Python 2 中,raw_input 将用户输入作为字符串返回,并且不会将其解析为数字类型.

You must know that in Python 2, raw_input returns the user input as string and does not parse it to a numeric type.

正如我所说,这意味着您将获得一个字符串列表.我们还可以看到您希望继续操作数字而不是字符串,因此最简单的方法是将输入转换为整数,然后再将它们附加到列表中:

That means you are getting a list of strings, as I said. We can also see that you want to continue operating on numbers and not on strings, so it's the easiest to just convert the input to integer numbers before appending them to the list:

Number_list.append(int(raw_input()))

这样我们就得到了一个数字列表,并且您代码后半部分的排序和计算都将按预期进行.

That way we get a list of numbers and both the sorting and your calculations in the second half of your code will work as expected.

如果我们对该列表进行排序,它将根据字符的 ASCII 代码按字母顺序对里面的字符串内容进行排序.并且1"的代码当然比5"或9"低,因此13"是结果的第一个元素.

If we sort that list, it will sort the string content inside alphabetically according to the ASCII codes of the characters. And '1' has of course a lower code than '5' or '9' and therefore '13' is the first element of the result.

您显然想按数字对列表进行排序,因此您需要直接用可能想要的数字填充列表,或者如果有特殊原因必须用字符串填充列表,则需要指定一个键将元素转换为数字的函数.

You obviously want to sort the list numerically, so you need to either directly fill the list with numbers which was probably intended, or if there's a special reason why the list has to be filled with strings, you need to specify a key function that converts the elements to numbers.

  • 选项 1(数字列表):

  • Option 1 (list of numbers):

Number_list = [9, 5, 13]
Number_list.sort()
print Number_list
# output: [5, 9, 13]

  • 选项 2(字符串列表,但带有排序键):

  • Option 2 (list of strings, but with sort key):

    Number_list = ['9', '5', '13']
    Number_list.sort(key=int)
    print Number_list
    # output: ['5', '9', '13']
    

  • 这篇关于我在python的排序功能有错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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