python第三天---collections类

时间:2021-10-16 06:37:31

collection系列

1.计数器(counter)

Counter是对字典类型的补充,用于追踪值的出现次数。

我们从中挑选一些相对常用的方法来举例:

python第三天---collections类

在上面的例子我们可以看出,counter方法返回的是一个字典,它将字符串中出现的所有字符都进行了统计。在这里再介绍一下update方法,这个update方法是将两次统计的结果相加,和字典的update略有不同。

2.有序字典(OrderedDict):

orderdDict是对字典类型的补充,主要是可以按照字典元素添加顺序输出。

普通字典:无序的,它依靠key,value之间的索引进行匹配。

 dict={                      
'':'test1',
'':'test2',
'':'test3',
'':'test4',
'':'test5',
'':'test6'
}
for k,v in dict.items():
print k,v

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAaoAAAClCAIAAACCzC7AAAAJI0lEQVR4nO3dzZHjOBKG4XGl5wC2ERsxMQboAFsUoi8MukK6Ale0B+oHAAEKRGK7tMi34jlVl7KyL18QBAr513/++fdl+P03ACjxF/EHQCfiD4BSxB8ApYg/AEoRfwCUIv4AKEX8AVCK+AOgFPEHQKk28WfnZba/fvw/83/dIYA/TBR/Zriu9+3LnQ0XY6d5bJlHyYKSDgH0rcHTnxmu6/lwsbNbm8bfQcG6DgH07Wfib3soaxh/xwWJPwB7fzr+zHCZ3T38Wm7D+7PGTq9/d/Ml+Oy4+B/bwu5jwbMdAlDip57+LrNLPKwZO7lneG0/80pAYyfnJvvMNTMufjjmClZ3CKB73xV/tzX4phmX+zPyovgrLFjdIYDufVH8pZaxwUrWPv458buIPwBnfV38fdwPeZ5lCd8YEn8ATvqi+Bt+/31b4+2O23hNVxiX+3r9WLC6QwDdaxF/dnLnT7G8ks7YafX3N7xS/lG+6FifnV0clKmCkg4B9K3VX32c/suK9wGXcEMjd/AlOvVy322DJAtKOgTQN648AKAU8QdAKeIPgFLEHwCliD8AShF/AJQi/gAoRfwBUIr4A6AU8QdAKeIPgFLEHwCliD8AShF/AJQi/gAoJY2/2/rtV+l9f4cAfoQo/vy76e3s9gN2Dxg7zU2vX04WlHQIoG/S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ml5GxdmXIJBSMk5v4IOAXRPHH/RMLayrdWDOb/+N/2CUfyVFJR0CKB70vgL0sp7dvvw2WRaJZaxwdL4Oa8yManyIP7qOgTQvQbv/t7faRF/H/dDtt8bvzFMF6zvEED3RAdfbmuwmdBk8Ru9m7uN6c2K6ETLQcG6DgF0T3buz3u5Vvjs9vJKOmOn1d/f8J7X/KN80bE+O7s4KPMF6zoE0DfpH73551ROJcv7g+HjWO7gS3TqZf8QV1KQ7APwwpUHAJQi/gAoRfwBUIr4A6AU8QdAKeIPgFLEHwCliD8ASv0X8gVk+RAarUEAAAAASUVORK5CYII=" alt="" />


有序字典:字典在内部自动维护了一个key列表。

import collections
dict1=collections.OrderedDict()
for lines in range(1,7):
dict1[str(lines)]='test%s'%(lines) for k,v in dict1.items():
print k,v

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXEAAACoCAIAAACZo0mgAAAHPUlEQVR4nO3dP24b6R0GYF1ltxjuIQIEixQBYqgYJAY2hoEg9RopVqAWcOXCnU9A8AY+A9lmbzGNfQ+mkEQPKUqab+bVx7HyCE9lUaOfCcyr+cfvvfjTn3++b/HTj/U17zZfPl+d5Vf/n+u/868+df/9dPn4iz+/++HsM8+N93DvYkaZsrj89Mfm18XLfK+bxdXnL7uvX0/48uXM/+v9O//qU3c70h+rV/dG+vXz7uvXndz3Hj5uRpkCvAAyBUiSKUCSTAGSZAqQJFOAJJkCJMkUIEmmAEkyBUiSKUCSTAGSZAqQFM6Udr1Zt7P+YPH8J4TvWiZTmsXVdnfz1ZXusU27Wl8nd/KTG5wyITBc8jilWVxty/fYdt1to5nyyAbHTQgMd+ZMuTl8CGbK4xuUKfDczpYpzeJy3e0OvzbL3rpYTbvaf79bH6zE11xv+j92kyBPbrB0QmCEsx+nXK67E4cVTbvq7hLh5jX7WGnaVdet2ruwaK43/cR5aIOjJwSKzDRTltuDf2yuN7u7HDnKlIEbHD0hUGSOmXLqLObgRKa9/faJ3yVT4LzmmylPXri9uz18eBVGps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RypR4t+noCYEigUxpFlfbuzquSKbEu02nTAgUSWTKUeHO4WWOJ362SrfplAmBIunnU3LnPsFu0ykTAkXOnCmLKt2mEycEhgs/n9LvHB3YH1qn23TKhMBwPkMIJMkUIEmmAEkyBUiSKUCSTAGSZAqQJFOAJJkCJMkUIEmmAEkyBUiSKUCSTAGSZAqQFFo3v7fWyf0VT+Zg/hPCyxDLlNH9pHW6TeO/BTjp/JlSp9tUpkAdoUy53ozLhWrdpqMnBIqkMmW1bm+aMYZeqqjcbTpiQmCE2HHK7lsVacGhR71u07ETAkWe5V5yu+76nTuPqNZtOnpCoMizZEo/Ap54ZZVu0ykTAkUifcmX6253UAaYyJRkt+mECYEioQ72w8sTkXOfYLfplAmBIplzn37BaNOuupKKvzrdplMmBIaLXU/p14YW3VKp1m06ekJgOJ8hBJJkCpAkU4AkmQIkyRQgSaYASTIFSJIpQJJMAZJkCpAkU4AkmQIkyRQgSaYASTIFSEpmynL74JomMzH/CeF7F+ri6NVl3CzUOLxAp1K36YQJgeFy/T5jl3et1G06YUJguNi6+eP+7NfpNp0yIVAkuW7+t6sVA44IanabjpsQGCGRKe2q2+26ruvv2wMPCup0m06ZECiSy5Sjpe0ndIYt0t2mUyYEisQy5SACekcZT/xslW7TKRMCRZ6lhzCSKdFu0/ETAkUy95KX22Rf8iLdbTplQqBI6PmU3gWLgUcZe3W6TadMCAwXeza/f+t3nt2moycEhvMZQiBJpgBJMgVIkilAkkwBkmQKkPQ/Tr4ZX5/l5DEAAAAASUVORK5CYII=" alt="" />

3.默认字典(defaultdict)

defaultdict是对字典的类型的补充,他默认给字典的值设置了一个类型。

有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。
即: {'k1': 大于66 , 'k2': 小于66}
常用方法:
dic={}
li=[11,22,33,44,55,66,77,88,99]
for item in li:
if item>66:
if 'k2'in dic.keys():
dic['k2'].append(item)
else:
dic['k2']=[item,] else:
if 'k1' in dic.keys():
dic['k1'].append(item)
else:
dic['k1']=[item

用默认字典实现

 import collections
value=[11,22,33,44,55,66,77,88,99]
dict1=collections.defaultdict(list)
for list1 in value:
if list1<=66:
dict1['k1'].append(list1)
else:
dict1['k2'].append(list1)
print dict1

4、可命名元组(namedtuple)

根据nametuple可以创建一个包含tuple所有功能以及其他功能的类型:

主要用于‘坐标’的表示。用法如下:

 import collections as coll
>>> mytuple=coll.namedtuple('mytuple',['x','y'])
>>> n=mytuple(1,2)
>>> n
mytuple(x=1, y=2)
>>> n.x
1
>>> n.y
2

5、双向队列(deque)

一个线程安全的双向队列:双向队列我们可以理解为两个栈底相连的栈,和队列的先进先出不同,元素可以从这个队列的两端分别加入或者删除值。

 a=coll.deque()
a.append(1)
a.append(2)
a.append(3)
print a.pop()
3
print a.popleft()
1