1111

>>> s='alex egon alex yuyang yuyang yuyang'
>>> s_ls = s.split()
>>> s_ls
['alex', 'egon', 'alex', 'yuyang', 'yuyang', 'yuyang']
>>> dic = {}
>>> for i in s_ls:
    if i not in dic:
        dic[i]=1
    else:
        dic[i]+=1

        
>>> dic
{'alex': 2, 'egon': 1, 'yuyang': 3}
>>> for i in dic.items():
    print(i)

    
('alex', 2)
('egon', 1)
('yuyang', 3)
>>> ls = []
>>> for i in dic.items():
    ls.append(i)

    
>>> ls
[('alex', 2), ('egon', 1), ('yuyang', 3)]
>>> ls.sort(key=lambda x:x[1])
>>> ls
[('egon', 1), ('alex', 2), ('yuyang', 3)]
>>> 

 

posted @ 2017-11-20 10:19  dragonbird  阅读(138)  评论(0编辑  收藏  举报