defaultdict()和namedtuple()是collections模塊裏面2個很實用的擴展類型。一個繼承自dict系統內置類型,一個繼承自tuple系統內置類型。在擴展的同時都添加了額外的很酷的特性,並且在特定的場合都很實用。mysql
defaultdict()
定義以及做用
返回一個和dictionary相似的對象,和dict不一樣主要體如今2個方面:sql
- 能夠指定key對應的value的類型。
- 沒必要爲默認值擔憂,換句話說就是沒必要擔憂有key沒有value這回事。總會有默認的value.
示例
defaultdict_egfrom collections import defaultdict
s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
d = defaultdict(list)
for k, v in s:
d[k].append(v)
print(list(d.items()))
d_2 = {}
for k, v in s:
d_2.setdefault(k, []).append(v)
print(list(d_2.items()))
d_3 = {}
for k, v in s:
d_3[k].append(v)
print(d_3.items())
輸出:shell
defaultdict_result[('red', [1]), ('blue', [2, 4]), ('yellow', [1, 3])]
[('red', [1]), ('blue', [2, 4]), ('yellow', [1, 3])]
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/Python Scripts/collection_eg.py", line 22, in <module>
d_3[k].append(v)
KeyError: 'yellow'
d = defaultdict(list),該語句建立一個defaultdict類型(你能夠想象爲dict類型),value的類型是list。經過對d_3的對比就能看到,defaultdict是能夠直接就進行d[k]的操做,即便d此時仍是一個空的字典。實際過程就是示例裏d_2的處理過程。數據庫
總結
主要使用根據數據建立字典時。當你須要爲一些數據生成字典,並且對值的類型進行限定的時候,考慮defaultdict。app
namedtuple()
定義及做用
namedtuple是繼承自tuple的子類。namedtuple和tuple比,有更多更酷的特性。namedtuple建立一個和tuple相似的對象,並且對象擁有能夠訪問的屬性。這對象更像帶有數據屬性的類,不過數據屬性是隻讀的。函數
示例
namedtuple_eg>>> from collections import namedtuple
>>> TPoint = namedtuple('TPoint', ['x', 'y'])
>>> p = TPoint(x=10, y=10)
>>> p
TPoint(x=10, y=10)
>>> p.x
10
>>> p.y
10
>>> p[0]
10
>>> type(p)
<class '__main__.TPoint'>
>>> for i in p:
print(i)
10
10
>>>
TPoint = namedtuple('TPoint', ['x', 'y']) 建立一個TPoint類型,並且帶有屬性x, y.fetch
經過上面的示例,能夠看出不只能夠經過p.x, p.y的方式訪問p的屬性,並且還可使用for來進行遍歷。這些就和tuple是同樣的。ui
還能夠經過設置參數來看namedtuple的全貌。this
namedtuple_eg2TPoint = namedtuple('TPoint', ['x', 'y'], verbose=True)
from builtins import property as _property, tuple as _tuple
from operator import itemgetter as _itemgetter
from collections import OrderedDict
class TPoint(tuple):
'TPoint(x, y)'
__slots__ = ()
_fields = ('x', 'y')
def __new__(_cls, x, y):
'Create new instance of TPoint(x, y)'
return _tuple.__new__(_cls, (x, y))
@classmethod
def _make(cls, iterable, new=tuple.__new__, len=len):
'Make a new TPoint object from a sequence or iterable'
result = new(cls, iterable)
if len(result) != 2:
raise TypeError('Expected 2 arguments, got %d' % len(result))
return result
def __repr__(self):
'Return a nicely formatted representation string'
return self.__class__.__name__ + '(x=%r, y=%r)' % self
def _asdict(self):
'Return a new OrderedDict which maps field names to their values'
return OrderedDict(zip(self._fields, self))
__dict__ = property(_asdict)
def _replace(_self, **kwds):
'Return a new TPoint object replacing specified fields with new values'
result = _self._make(map(kwds.pop, ('x', 'y'), _self))
if kwds:
raise ValueError('Got unexpected field names: %r' % list(kwds))
return result
def __getnewargs__(self):
'Return self as a plain tuple. Used by copy and pickle.'
return tuple(self)
x = _property(_itemgetter(0), doc='Alias for field number 0')
y = _property(_itemgetter(1), doc='Alias for field number 1')
這裏就顯示出了namedtuple的一些方法。很明顯的看到namedtuple是直接繼承自tuple的。spa
幾個重要的方法:
1.把數據變成namedtuple類:


namedtuple_eg3>>> TPoint = namedtuple('TPoint', ['x', 'y'])
>>> t = [11, 22]
>>> p = TPoint._make(t)
>>> p
TPoint(x=11, y=22)
>>>
2. 根據namedtuple建立的類生成的類示例,其數據是隻讀的,若是要進行更新須要調用方法_replace.
namedtuple_eg4>>> p
TPoint(x=11, y=22)
>>> p.y
22
>>> p.y = 33
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
p.y = 33
AttributeError: can't set attribute
>>> p._replace(y=33)
TPoint(x=11, y=33)
>>>
3.將字典數據轉換成namedtuple類型。
namedtuple_eg5>>> d = {'x': 44, 'y': 55}
>>> dp = TPoint(**d)
>>> dp
TPoint(x=44, y=55)
>>>
總結
namedtuple最經常使用仍是出如今處理來csv或者數據庫返回的數據上。利用map()函數和namedtuple創建類型的_make()方法。
namedtuple_eg6EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade')
import csv
for emp in map(EmployeeRecord._make, csv.reader(open("employees.csv", "rb"))):
print(emp.name, emp.title)
# sqlite數據庫
import sqlite3
conn = sqlite3.connect('/companydata')
cursor = conn.cursor()
cursor.execute('SELECT name, age, title, department, paygrade FROM employees')
for emp in map(EmployeeRecord._make, cursor.fetchall()):
print(emp.name, emp.title)
# MySQL 數據庫
import mysql
from mysql import connector
from collections import namedtuple
user = 'herbert'
pwd = '######'
host = '127.0.0.1'
db = 'world'
cnx = mysql.connector.connect(user=user, password=pwd, host=host,database=db)
cur.execute("SELECT Name, CountryCode, District, Population FROM CITY where CountryCode = 'CHN' AND Population > 500000")
CityRecord = namedtuple('City', 'Name, Country, Dsitrict, Population')
for city in map(CityRecord._make, cur.fetchall()):
print(city.Name, city.Population)