python统计文件行数和字符数组_使用Python查找文件大小的字符串中数组字符串的频率...

我看了很多答案,它们的目标是找到文件中每个单词的出现,或者大字符串,甚至数组。但我不想这么做,我的字符串也不是来自文本文件。在

给定一个大字符串,比如一个文件大小的字符串,如何计算大字符串中每个数组元素的频率(包括单词中的空格)?在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

现在是十一月,洗衣店和通道,但不是'父亲有'。我需要能够找到带空格的字符串元素。我知道这是因为我将上下文拆分为“”返回“父”“had”,那么如何恰当地拆分上下文,还是将其与regex findall一起使用?在

编辑:

使用上下文作为一个大字符串,我有:

^{pr2}$

退货:Counter({'Laundress': 0, 'November': 0, 'Father had': 0, 'Passage': 0})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值