在Python排序列表時類型錯誤時,我試圖返回:試圖從一個文件
- 唯一字
- 發生次數的文件中的計數排序列表
我不斷收到錯誤:
TypeError: '<' not supported between instances of 'int' and 'str'.
我的代碼如下:
def countWords(ifile):
lst1=[]
infile=open(ifile,'r')
lines=(inifle.read()).lower()
for element in lines.split():
lines.replace(',',' ')
sct=lines.count(element)
lst1.append(element)
lst1.append(sct)
return lst1.sort()
infile.close()
我在做什麼錯?
錯誤告訴你什麼是錯的:你試圖理清同時包含字符串和數字的列表。什麼應該考慮更大'9'或''狗'? – Julien
'狗'將被視爲更大 –
如果您的問題得到解答,您可以[接受最有幫助的](https://stackoverflow.com/help/someone-answers)。 –