码迷,mamicode.com
首页 > 编程语言 > 详细

python统计字符串中每个单词出现的个数【一行】

时间:2020-02-26 20:37:06      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:isp   control   col   lambda   split()   and   字符串   controls   set   

s = ‘i am very very like you and like you‘

dict( [(i, s.split().count(i)) for i in s.split()] )
Out[2]: {‘i‘: 1, ‘am‘: 1, ‘very‘: 2, ‘like‘: 2, ‘you‘: 2, ‘and‘: 1}
set( map(lambda x:(x, s.split().count(x)), s.split()) )
Out[6]: {(‘am‘, 1), (‘and‘, 1), (‘i‘, 1), (‘like‘, 2), (‘very‘, 2), (‘you‘, 2)}
dict( map(lambda x:(x, s.split().count(x)), s.split()) )
Out[7]: {‘i‘: 1, ‘am‘: 1, ‘very‘: 2, ‘like‘: 2, ‘you‘: 2, ‘and‘: 1}

 

python统计字符串中每个单词出现的个数【一行】

标签:isp   control   col   lambda   split()   and   字符串   controls   set   

原文地址:https://www.cnblogs.com/douzujun/p/12368652.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!