collections

  • 引入

    import collections

  • Counter

    most_common

  • In [6]: collections.Counter('abcdeabcdabcaba').most_common(3)

  • Out[6]: [('a', 5), ('b', 4), ('c', 3)]


  • In [7]: collections.Counter('rommel').most_common(3)

  • Out[7]: [('m', 2), ('e', 1), ('r', 1)]


  • In [8]: collections.Counter('rommel').most_common(1)

  • Out[8]: [('m', 2)]


    update

In [10]: c1 = collections.Counter('aabc')


In [11]: c2 = collections.Counter('aac')


In [12]: c1

Out[12]: Counter({'a': 2, 'c': 1, 'b': 1})


In [13]: c2

Out[13]: Counter({'a': 2, 'c': 1})


In [14]: c1.update(c2)


In [15]: c1

Out[15]: Counter({'a': 4, 'c': 2, 'b': 1})


  • OrderedDict

    有序字典

  • In [19]: o1= collections.OrderedDict()


  • In [20]: o1['k1']=2


  • In [21]: o1['k2']=2


  • In [22]: o1

  • Out[22]: OrderedDict([('k1', 2), ('k2', 2)])


    默認字典

  • defaultdict(list)

  • defaultdict(tuple)

感覺這個不太喜歡 可能是自己scala寫多了 感覺好廢物的樣子


wKiom1a0Nn_hBLgnAALRRV55H5I761.jpg






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章