python - 使用 Python 查找文件大小的字符串中数组字符串的频率

标签 python regex arrays string

我查看了很多答案,其目的是查找文件中每个单词的出现情况,或者大字符串甚至数组。但我不想这样做,我的字符串也不是来自文本文件。

给定一个大字符串,例如文件大小的字符串,您如何计算大字符串中每个数组元素的频率 - 包括单词中的空格?

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/

相关文章:

sql - LIKE 和 ~ 在 Postgres 中的区别

java - 我需要什么 Java 正则表达式来匹配此文本?

javascript - 简化此正则表达式以避免术语重复

java - 如何从列表创建对象的二维数组?

c++ - 在同一结构中声明一个结构数组

python - 在 Mac OS 10.13.5 上安装后,fabric 几乎无法工作

python - 使用 srid 4326 进行 GeoDjango 距离查询返回 'SpatiaLite does not support distance queries on geometry fields with a geodetic coordinate system.'

python - 读取以3个空格分隔的文本文件数据

python - Python/OpenCV网络摄像头向后移动

javascript - 如何对数组等对象列表进行排序