Python3.4 统计字符个数 5
求以下问题要怎么写啊:Defineafunctionwhich,givenastringargument,countsthetotalnumberoftimesevery...
求以下问题要怎么写啊:
Define a function which, given a string argument, counts the total number of times every word appears in the string. For this problem, take a very simplified definition of a word as a contiguous subsequence of letters. Any non-letter character is taken as a word boundary. Upper and lower case letters are equivalent.
大神们帮帮忙啊!!!刚学python完全不知道怎么写啊!!!江湖救急啊!! 展开
Define a function which, given a string argument, counts the total number of times every word appears in the string. For this problem, take a very simplified definition of a word as a contiguous subsequence of letters. Any non-letter character is taken as a word boundary. Upper and lower case letters are equivalent.
大神们帮帮忙啊!!!刚学python完全不知道怎么写啊!!!江湖救急啊!! 展开
1个回答
展开全部
定义一个函数,给定一个字符串参数,计算每个单词出现在字符串总次数 不区分大小写 。还有其他什么要求没看懂
##python 2.7 windows
words =raw_input (' please enter words:')
countdict={}
for word in words.strip():
word=word.lower()
if countdict.has_key(word):
countdict[word]+=1
else:
countdict[word]=1
print countdict
--------------------------
>>>
please enter words:asdasdnm,.nm.zxcnm,
{'a': 2, 'c': 1, 'z': 1, 'd': 2, 'm': 3, ',': 2, 'n': 3, 's': 2, 'x': 1, '.': 2}
>>>
##python 2.7 windows
words =raw_input (' please enter words:')
countdict={}
for word in words.strip():
word=word.lower()
if countdict.has_key(word):
countdict[word]+=1
else:
countdict[word]=1
print countdict
--------------------------
>>>
please enter words:asdasdnm,.nm.zxcnm,
{'a': 2, 'c': 1, 'z': 1, 'd': 2, 'm': 3, ',': 2, 'n': 3, 's': 2, 'x': 1, '.': 2}
>>>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询