如何编写此代码以获得类似的输出,但在垂直行中;这是我的输出

2024-05-13 22:34:18 发布

您现在位置:Python中文网/ 问答频道 /正文

def main():
    myfile = input("Please enter a File: ")
    open_file = open(myfile, "r")
    mylexicon = {}
    mytext = open_file.readlines()
    acc = 0
    for line in mytext:
        words = line.split()
        for word in words:
            if word not in mylexicon:
                mylexicon[word] = 1
            else:
                mylexicon[word] = mylexicon[word] + 1
    mylexicon = mylexicon.readline()
    print(mylexicon)

main()

上面的代码生成输出:

{'mouse': 2, 'last': 1, 'and': 1, 'jumped': 1, 'over': 1, 'Zippy': 1, 'the': 2, 'cat': 3, '1': 1, '2': 1}

但我想把它列在一条垂直线上


Tags: inforinputmaindeflineopenmyfile
1条回答
网友
1楼 · 发布于 2024-05-13 22:34:18

使用(如果愿意,可以添加冒号和引号…)

for word, number in mylexicon.iteritems():
    print(word, number)

而不是

print(mylexicon)

相关问题 更多 >