【发布时间】:2019-11-26 02:30:16
【问题描述】:
我想创建一个函数;
读取文本文件,逐字读取文件并将单词存储在数组中。
计算每个单词出现的次数,并且每个单词只在数组中存储一次。
输出每个单词,旁边显示其出现次数。
示例:
文本文件说:“弗兰克吃豌豆吃薯条豌豆”
将创建一个数组,[Frank, eats, peas, eats, fries, peas]
然后,将打印最终产品;
弗兰克 1 吃 2 豌豆 2 薯条 1
--
这就是我目前所拥有的
def countWordsInFile():
array = []
array2 = [1,2,3,4,5]
length = len(array)
fileName = getUserText("Enter the name of the file you want to read array from")
openFile = openNewFile(fileName,"read")
i = openFile
for words in i.read().split():
array.append(words)
print(array)
for i in range(0,length,1):
count = array.count[i]
array2.append(count)
print(array2)
【问题讨论】:
标签: python arrays list text-files