我查看了很多答案,其目的是查找文件中每个单词的出现情况,或者大字符串甚至数组。但我不想这样做,我的字符串也不是来自文本文件。
给定一个大字符串,例如文件大小的字符串,您如何计算大字符串中每个数组元素的频率 - 包括单词中的空格?
def calculate_commonness(context, links):
c = Counter()
content = context.translate(string.maketrans("",""), string.punctuation).split(None)
for word in content:
if word in links:
c[word] += 1
print c
context = "It was November. Although it was November November November Passage not yet late, the sky was dark when I turned into Laundress Passage. Father had finished for the day, switched off the shop lights and closed the shutters; but so I would not come home to darkness he had left on the light over the stairs to the flat. Through the glass in the door it cast a foolscap rectangle of paleness onto the wet pavement, and it was while I was standing in that rectangle, about to turn my key in the door, that I first saw the letter. Another white rectangle, it was on the fifth step from the bottom, where I couldn\'t miss it."
links = ['November', 'Laundress', 'Passage', 'Father had']
# My output should look (something) like this:
# November = 4
# Laundress = 1
# Passage = 2
# Father had = 1
目前正在查找“November”、“Laundress”和“Passage”,但没有查找“Father had”。我需要能够找到带空格的字符串元素。我知道这是因为我用“”分割上下文,返回“Father”“had”,那么如何正确分割上下文或者将其与正则表达式 findall 一起使用?
编辑: 使用上下文作为一个大字符串我有:
for l in links:
c[l] = context.lower().count(l)
print c
返回:
Counter({'Laundress': 0, 'November': 0, 'Father had': 0, 'Passage': 0})
最佳答案
你试过吗
context.lower()
counts = {word: context.count(word)
for word in links}
注意:将context
保留为字符串。
关于python - 使用 Python 查找文件大小的字符串中数组字符串的频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20763989/