Quick Stats sort in python dictionary, strings, lists of

The collections Python modules can be quickly dictionary, strings, lists, statistics and sort

First, sort the list by statistics

import collections
# 对列表进行统计
obj = collections.Counter(['a', 'a', 'a', 'b', 'b', 'c'])
print(obj)

The results are:

Counter({'a': 3, 'b': 2, 'c': 1})

Second, the string sorting statistics

import collections
# 对字符串进行统计
obj = collections.Counter('aabbccccdddds')
print(obj)

result:

Counter({'c': 4, 'd': 4, 'a': 2, 'b': 2, 's': 1})

Third, the dictionary sorted by value

from collections import OrderedDict
d ={'banana':3,'apple':4,'pear':1,'orange':2}
od = OrderedDict(sorted(d.items()))
print(od)

result:

OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])

 

Guess you like

Origin blog.csdn.net/a857553315/article/details/93774033