小编use*_*645的帖子

python中的属性错误不会消失

为什么我一直这样

AttributeError: 'dict_keys' object has no attribute 'sort'
Run Code Online (Sandbox Code Playgroud)

还是我的代码?我怎样才能解决这个问题?

import string

infile = open('alice_in_wonderland.txt', 'r')

text = infile.readlines()

counts = {}

for line in text:
    for word in line:
    counts[word] = counts.get (word, 0) +1
'''
if word != " ":
if word != ".":
'''         

word_keys = counts.keys()
word_keys.sort()

infile.close()

outfile = open("alice_words.txt", 'w')
outfile.write("Word \t \t Count \n")
outfile.write("======================= \n")
for word in word_keys:
outfile.write("%-12s%d\n" % (word.lower(), counts[word]))
outfile.close()
Run Code Online (Sandbox Code Playgroud)

我不知道还能做什么.

python sorting dictionary

2
推荐指数
1
解决办法
1056
查看次数

替换Python中的字符串中的字符

我想替换字符串中的字符,不使用字符串函数或类似string.replace.

 >>> replace ("banana", "a", "e")
'benene'
Run Code Online (Sandbox Code Playgroud)

因此,对于该示例,我想在字符串"banana"中将字符"a"替换为"e"

python string replace

0
推荐指数
1
解决办法
82
查看次数

标签 统计

python ×2

dictionary ×1

replace ×1

sorting ×1

string ×1