我正在尝试完成家庭作业,我需要将数组中 5 个项目的组合与文件列表匹配。
"Orange, banana, cat, apple, strings"
我有一个包含所有这些单词的文件,以及它们的组合
ex:
1: Orange
2: Banana
3: CatBanana
我需要单独计算所有这些
`ex: Orange=1, Banana=1 CatBanana=1`
str.count((
使用 str.count(s)
计算 Str 中 s 出现的次数:
datafile = 'data.txt'
items = "Orange banana cat apple strings".lower().split()
counts = dict.fromkeys(items, 0)
with open(datafile, 'rt') as f:
for line in f:
line = line.lower()
for item in items:
counts[item] += line.count(item)
print(counts)
我假设您正在尝试查找单词列表在文件中出现的次数(逐行(?
w = ['some', 'list', 'of', 'words']
r = dict.fromkeys(w,0) # used to store values
filename = 'file.txt'
with open(filename, 'r') as f: # open file
lines = f.readlines() # file to list
for string in lines:
for word in w:
if word in string:
r[word] += 1 # update
print(r)
如果没有,那么我会看看这篇文章:将文件中的单词读入字典
编辑:
只需格式化单词数组以匹配您尝试查找的单词或小写所有内容,这样格式化就不会妨碍(就像 RootTwo 所做的那样(。我相信您也在尝试将文件与数组进行比较,以便:
# fix formatting for comparison.
format_this = "Orange, banana, cat, apple, strings" # lowercase if necessary
separate = format_this.split(', ') # ['Orange', 'banana', 'cat', 'apple', 'strings']
compare = [word.capitalize() for word in separate] # ['Orange', 'Banana', 'Cat', 'Apple', 'Strings']
# set file to dict.
filename = 'file.txt'
lines = []
with open(filename, 'r') as f: # open file
lines = f.readlines()
lines = [line.strip('n') for line in lines] # clean up, lowercase if necessary
r = dict.fromkeys(lines,0) # {'Orange': 0, 'Banana': 0, 'CatBanana': 0}
# compare and record.
for key in r:
for word in compare:
if word in key: # match found
r[key] += 1 # update value
break # stop and move on once match found, remove if necessary
print(r) # {'Orange': 1, 'Banana': 1, 'CatBanana': 1}