返回

python-当 str 对象属性为“只读”时,这意味着什么?如何修复我的代码?

发布时间:2022-07-19 15:50:12 425
# node.js

这是我的代码:

# Get the name of the file and open it
name = input("Enter file:")
handle = open(name)

# Count word frequency 
counts = dict()
for line in handle:
    words = line.split()
    for word in words:
        counts[word] = counts.get(word,0) + 1
        
# Find the most common word
bigcount = None
bigword = None 
for word.count in counts.items():
    if bigcount is None or count > bigcount:
        bigword = word
        bigcount = count
        
# All done
print(bigword, bigcount)

当我调用它并输入消息并按enter键时,会出现AttributeError并说:

 ---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
 in 
     13 bigcount = None
     14 bigword = None
---> 15 for word.count in counts.items():
     16     if bigcount is None or count > bigcount:
     17         bigword = word

AttributeError: 'str' object attribute 'count' is read-only

我应该怎么做才能修复我的代码?什么是“只读”str 对象属性?

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像