栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

使用字符串(2)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用字符串(2)

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)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/603092.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号