# coding: utf-8

# In[1]:

import collections

str = "Be slow to promise and quick to perform"
# 按空格切割
str_split = str.split(' ')


# In[2]:

str_split

# Out[2]:
#['Be', 'slow', 'to', 'promise', 'and', 'quick', 'to', 'perform']

# In[3]:

# 统计每个单词的个数
temp_str = collections.Counter(str_split).most_common()
temp_str

# Out[3]:
# [('to', 2),
#  ('and', 1),
#  ('Be', 1),
#  ('slow', 1),
#  ('perform', 1),
#  ('promise', 1),
#  ('quick', 1)]

# In[4]:

# 排序方式用lambda ,先排个数,再按字母顺序排
sorted(temp_str, key = lambda x:[-x[1],x[0]])

# Out[4]:
# [('to', 2),
#  ('Be', 1),
#  ('and', 1),
#  ('perform', 1),
#  ('promise', 1),
#  ('quick', 1),
#  ('slow', 1)]

 

 posted on 2018-03-12 23:23  天进鱼  阅读(5042)  评论(0编辑  收藏  举报