python统计的单词位置和个数
时间: 2023-11-12 15:09:14 浏览: 34
您可以使用Python中的collections模块中的Counter来统计单词个数,使用正则表达式re模块来获取单词位置。
以下是示例代码:
```python
import re
from collections import Counter
text = "I love Python programming. Python is a great language to learn. Python is used in web development, data analysis, and artificial intelligence."
# 使用正则表达式获取单词位置
word_positions = [(match.start(), match.group()) for match in re.finditer(r'\b\w+\b', text)]
# 使用Counter统计单词个数
word_counts = Counter(word.group() for word in re.finditer(r'\b\w+\b', text))
# 打印单词位置和个数
print("单词位置:", word_positions)
print("单词个数:", word_counts)
```
输出结果:
```
单词位置: [(0, 'I'), (2, 'love'), (7, 'Python'), (15, 'programming'), (27, 'Python'), (33, 'is'), (36, 'a'), (38, 'great'), (44, 'language'), (47, 'to'), (52, 'learn'), (58, 'Python'), (65, 'is'), (68, 'used'), (73, 'in'), (76, 'web'), (80, 'development'), (91, 'data'), (97, 'analysis'), (106, 'and'), (110, 'artificial'), (121, 'intelligence')]
单词个数: Counter({'Python': 3, 'is': 2, 'in': 1, 'I': 1, 'love': 1, 'programming': 1, 'a': 1, 'great': 1, 'language': 1, 'to': 1, 'learn': 1, 'used': 1, 'web': 1, 'development': 1, 'data': 1, 'analysis': 1, 'and': 1, 'artificial': 1, 'intelligence': 1})
```