1.编写程序 - 词频统计
‘’’
功能:词频统计
时间:2021年11月25日
‘’’
text = ‘I love python I love java I learn python’
words = text.split(’ ')
diff_words = list(set(words))
counts = []
for i in range(len(diff_words)):
counts.append(0)
for i in range(len(words)):
for j in range(len(diff_words)):
if diff_words[j] == words[i]:
counts[j] = counts[j] + 1
for word_count in zip(diff_words, counts):
print(word_count)
2.检索子串出现次数
text = ‘I love python I love java I learn python’
words = text.split(’ ‘)
words.count(‘love’)
2
words.count(‘I’)
3
3.输出子串的所有位置
‘’’
功能:输出子串的所有位置
时间:2021年11月25日
‘’’
at_str = ‘@娃哈哈 @萌萌哒 @小机灵 @小宝宝’
pos = at_str.find(’@’)
count=0
while pos != -1:
count = count + 1
print(’@出现位置:{}’.format(pos))
pos = at_str.find(’@’, pos + 1)
print(’@总共出现了{}次’.format(count))
print(’@总共出现了{}次’.format(at_str.count(’@’)))
4.搜索全部MP3类型文件名
‘’’
功能:搜索全部MP3类型文件名
时间:2021年11月25日
‘’’
files = [‘通知.txt’, ‘两只蝴蝶.MP3’, ‘青花瓷.exe’, ‘喜欢你.MP3’, ‘童年.MP3’]
music_list = []
for file in files:
if file.lower().endswith(’.mp3’):
music_list.append(file.lower())
print(music_list)



