因为是NoneType,所以无法对列表进行排序?简单的Python

发布于 2021-01-29 18:23:37

当我尝试为我的BeautifulSoup网络刮板找出低价和高价时,出现此错误。我附上了下面的代码。我的列表不应该是整数列表吗?

在发布此消息之前,我经历了类似的NoneType问题,但是解决方案不起作用(或者我听不懂!)

Traceback (most recent call last):
  File "/home/user-machine/Desktop/cl_phones/main.py", line 47, in <module>
    print "Low: $" + intprices[0]
TypeError: 'NoneType' object is not subscriptable

相关摘要:

intprices = []
newprices = prices[:]
total = 0
for k in newprices:
    total += int(k)
    intprices.append(int(k))

avg = total/len(newprices)

intprices = intprices.sort()

print "Average: $" + str(avg)
print "Low: $" + intprices[0]
print "High: $" + intprices[-1]
关注者
0
被浏览
46
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    intprices.sort()在原位排序并返回None,同时sorted( intprices )从列表中创建一个全新的排序列表并返回。

    在您的情况下,由于您不想保留intprices原始格式,只需intprices.sort()不重新分配就可以解决您的问题。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看