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

Tyl*_*our 6 python list nonetype

当我试图找出我的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
Run Code Online (Sandbox Code Playgroud)

相关代码段:

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]
Run Code Online (Sandbox Code Playgroud)

sou*_*kah 17

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

在您的情况下,因为您不想保持intprices其原始形式,只需在intprices.sort()不重新分配的情况下解决您的问题.


wim*_*wim 6

你的问题是这样的:

intprices = intprices.sort()

.sort()名单上的方法,就地,并返回列表上运行None.只需将其更改为:

intprices.sort()