python - 比较列表中的元素

标签 python list python-3.x

我刚开始学习 Python...我正在编写一个简单的程序,它将接受整数并保留一个未排序和已排序的列表。我在排序列表部分有问题...... 比较列表中元素的值时出现错误。我得到错误的地方是以下行:“if sortedList[sortcount] > sortedList[count]:”。我得到“TypeError:不可排序的类型:list() > int()”。

这是部分代码...我不确定哪里出了问题。

numberList = []
sortedList = []
count = 0
sum = 0

....(skip)....

sortcount = 0
sortedList += [ int(userInput) ]
while sortcount < count:
    if sortedList[sortcount] > sortedList[count]:
        sortedList[count] = sortedList[sortcount]
        sortedList[sortcount] = [ int(userInput) ]  
    sortcount+=1

最佳答案

你在哪里:

sortedList[sortcount] = [ int(userInput) ]

你应该这样做:

sortedList[sortcount] = int(userInput)

否则你会在该位置添加一个列表并给出你告诉的错误。

顺便说一句,while循环之前的第一行最好写

sortedList.append(int(userInput))

关于python - 比较列表中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8865509/

相关文章:

python - SQLite - 聚集列存储索引

R 中的 R 绑定(bind)和合并

android - 如何将以下代码分解为android中的各个类

列表 Monad : Consequences of the signature of List. flatMap(f: (A) ⇒ GenTraversableOnce[B])

python-3.x - 这是如何在 Python 3 下使用 PyEphem 计算格林威治时角?

python - Matplotlib 范围与 mollweide 投影

Windows 7 中的 Python 脚本错误

python - 2D 和 3D 数组之间的点积

python - 如何获取python顶层程序的顶层变量列表?

Python2.7 日志级别的日志记录不起作用