Переопределение CSS в дочерней теме WordPress TwentyTen не вступает в силу

Используйте объект collections.Counter() и разделите слова на пробелы. Возможно, вы захотите также сгладить ваши слова и удалить пунктуацию:

from collections import Counter

counts = Counter()

for sentence in sequence_of_sentences:
    counts.update(word.strip('.,?!"\'').lower() for word in sentence.split())

или, возможно, использовать регулярное выражение, которое соответствует только символам слов:

from collections import Counter
import re

counts = Counter()
words = re.compile(r'\w+')

for sentence in sequence_of_sentences:
    counts.update(words.findall(sentence.lower()))

Теперь у вас есть counts словарь с подсчетом слов.

Демо:

>>> sequence_of_sentences = ['This is a sentence', 'This is another sentence']
>>> from collections import Counter
>>> counts = Counter()
>>> for sentence in sequence_of_sentences:
...     counts.update(word.strip('.,?!"\'').lower() for word in sentence.split())
... 
>>> counts
Counter({'this': 2, 'is': 2, 'sentence': 2, 'a': 1, 'another': 1})
>>> counts['sentence']
2
0
задан lordchancellor 22 February 2015 в 16:05
поделиться