计算另一个字符串中多个字符串的出现 [英] Counting occurrences of multiple strings in another string

查看:90
本文介绍了计算另一个字符串中多个字符串的出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python 2.7中,给出以下字符串:

In Python 2.7, given this string:


Spot是一只棕色的狗。斑点有棕色的头发。 Spot的头发是棕色的。

Spot is a brown dog. Spot has brown hair. The hair of Spot is brown.

什么是找到 Spot,棕色总金额的最佳方法? s和字符串中的 hair?在示例中,它将返回8。

what would be the best way to find the sum amount of "Spot"s, "brown"s, and "hair"s in the string? In the example, it would return 8.

我正在寻找类似 string.count( Spot, brown,头发),但可以与元组或列表中的要查找的字符串一起使用。

I'm looking for something like string.count("Spot","brown","hair") but works with with the "strings to be found" in a tuple or list.

谢谢!

推荐答案

这可以满足您的要求,但是请注意,它还会计算诸如 hairy, browner等单词。

This does what you asked for, but notice that it will also count words like "hairy", "browner" etc.

>>> s = "Spot is a brown dog. Spot has brown hair. The hair of Spot is brown."
>>> sum(s.count(x) for x in ("Spot", "brown", "hair"))
8

您也可以将其写为地图

>>> sum(map(s.count, ("Spot", "brown", "hair")))
8

更强大的解决方案可以使用 nltk软件包

A more robust solution might use the nltk package

>>> import nltk  # Natural Language Toolkit
>>> from collections import Counter
>>> sum(x in {"Spot", "brown", "hair"} for x in nltk.wordpunct_tokenize(s))
8

这篇关于计算另一个字符串中多个字符串的出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆