wlbk.net
当前位置:首页 >> python 统计一句话中最长单词 >>

python 统计一句话中最长单词

if __name__ == '__main__': say_words = rawinput(“请输入要测试的一句话”) word_list = say_words.split(',') //用逗号转换为列表 word_len_list = [len(word) for word in say_words] max_word_len = max(word_len_list) for word in say_wor...

众所周知,英文是以词为单位的,词和词之间是靠空格隔开,而中文是以字为单位,句子中所有的字连起来才能描述一个意思。例如,英文句子I am a student,用中文则为:“我是一个学生”。计算机可以很简单通过空格知道student是一个单词,但是不能很...

content = {} wth open("文件") as fr: for line in fr: lines = line.strip().split(" ") #假设单词与单词之间,空格做为分隔符 for word in lines: if word not in content: content[word] = 0 content[word] += 1 for word,val in content.it...

"fatway" 的方法简单-美。 还有另一中方法:引入collections的Counter实现更强大的功能 import collectionsimport repatt = re.compile("\w+")counter = collections.Counter(patt.findall( open('reparser.py','rt').read() ))# top 10for word...

def num_input(): count=1 data_list=[] while True: count += 1 data=input("enter a number:") if data == "": print(count-2,"numbers entered") break number=float(data) data_list.append(number) print("sum:",sum(data_list),"max:", ma...

count=0preMajor=''for major in majors: if major==preMajor: count+=1 else: if len(preMajor)>0: print(preMajor,count) preMajor=major count=1 if len(preMajor)>0: print (preMajor,count)

(你确定是单词不是字母?) 1.(python可以用中文变量,看不顺眼就换成英文) 句子 = "This is a sentence" #可以写别的字典 = {}句子拆成的单词列表 = 句子.split()for 单词 in 句子拆成的单词列表:if 单词 in 字典.keys():字典[单词] += 1els...

使用比较基本的方法写的参考代码: #!/usr/bin/env python# -*- coding: utf-8 -*-#python 2.7import reprint u'请输入字符串:'wz = raw_input()s = wz.lower()#小写单词的正则表达式r='[a-z]+'#找到所有单词ws = re.findall(r,s)#定义一个字典...

# Take English sentence for exampleasentence = "I am an engineer!"print len(asentence.split())

python有个特别简单的方法就可以实现,直接用str的count方法就可以了,如下

网站首页 | 网站地图
All rights reserved Powered by www.wlbk.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com