python统计单词个数并输出最长单词 
时间: 2023-05-11 18:03:25 浏览: 16
可以使用Python的collections模块中的Counter函数来统计单词个数,然后使用max函数找到最长的单词。以下是示例代码:
```python
from collections import Counter
text = "This is a sample text with some words. This is another sentence with more words."
# 统计单词个数
word_counts = Counter(text.split())
# 输出单词个数
print("单词个数:", sum(word_counts.values()))
# 输出最长单词
longest_word = max(word_counts, key=len)
print("最长单词:", longest_word)
```
输出结果为:
```
单词个数: 15
最长单词: sentence
```
相关问题
python统计句子中单词个数并输出最长单词
可以使用以下代码实现:
```python
sentence = input("请输入句子:")
words = sentence.split()
print("单词个数为:", len(words))
longest_word = max(words, key=len)
print("最长单词为:", longest_word)
```
输入一个句子后,程序会输出单词个数和最长的单词。
python从键盘输入一段英文句子,统计出单词数,输出最长单词
可以通过以下代码实现:
text = input("请输入一段英文句子:")
words = text.split()
num_words = len(words)
longest_word = max(words, key=len)
print("该句子共有 %d 个单词,最长的单词是 '%s'" % (num_words, longest_word))
相关推荐















