字符串: 'Speak words of kindness kindness' 要求:将此字符串中单词和单词出现的次数输出到字典中。 判断有多少个空格? 以相反的顺序输出字符Speak应如何?
时间: 2024-02-26 19:51:36 浏览: 143
这个字符串中有4个空格。以相反的顺序输出字符'Speak'应该是 'kaepS'。
以下是将单词和单词出现次数输出到字典中的Python代码:
```python
sentence = 'Speak words of kindness kindness'
word_list = sentence.split()
word_dict = {}
for word in word_list:
if word not in word_dict:
word_dict[word] = 1
else:
word_dict[word] += 1
print(word_dict)
```
输出结果为:{'Speak': 1, 'words': 1, 'of': 1, 'kindness': 2}
相关问题
As one saying goes, no act of kindness, no matter how small, is ever wasted. How do you think about it?
I completely agree with that saying. Even the smallest act of kindness can make a big difference in someone's day or even their life. It doesn't have to be a grand gesture or a large donation. A simple smile, a kind word, or a small act of service can make a world of difference to someone who may be going through a tough time. Kindness is contagious and can inspire others to pay it forward, creating a ripple effect of positivity and compassion. So, no act of kindness is ever wasted, and we should always strive to spread kindness wherever we go.
阅读全文