1. 程式人生 > >python 命名元組(namedtuple)

python 命名元組(namedtuple)

我們知道c/c++語言中,有結構體這種資料型別:

struct{
        string name;
        int age;
        char sex;
   }student;
在對結構體物件進行賦值或者取值時可以使用.運算子進行操作。
那麼問題來,python中有沒有這個資料型別呢?答案是肯定有的,它就是命名元組(namedtyple)。

首先來看一下python中普通元組的不方便之處:

Bob=("bob",30,'male')
#如果想知道Bobde 名字,則需要使用索引位置進行讀取,如下
name=Bob[0]
for people in [Bob]:
    print
("%s is %d years old %s" % peole) #顯示結果 bob is 30 years old male

1、namedtuple基礎

通過上面的例子,訪問元祖資料的時候是通過索引下標來操作的,對此需要熟記每個下標對應的具體含義,如果元祖有成千上百個資料,那麼想記住每個下標對應的意義那是相當困難的,於是就出現了命名元祖namedtuple。

namedtuple物件的定義如以下格式:

collections.namedtuple(typename, field_names, verbose=False, rename=False) 

返回一個命名元祖子類typename,其中引數的意義如下:
typename,:此元組的名稱;
field_names: 元祖中元素的名稱(類似於c結構體中的age等),此欄位有多種表達方式,見例子;
rename:如果元素名稱中含有python的關鍵字,則必須設定為rename=True,具體見下面;
verbose:預設就好;

舉個小例子,加深一下自己的理解:

import collections
#其中field_names 有多種表達方式,如下
student=collections.namedtuple('student','name age sex')
student=cpllections.namedtuple('student',['name','age','sex'])
student=cpllections.namedtuple('student','name,age,sex')

spark=student(name='sunYang',age=20,sex='male')
print(spark)
print
("spark's name is %s" % spark.name) print("%s is %d years old %s" % spark) 顯示結果如下: student(name='sunYang', age=20, sex='male') spark's name is sunYang sunYang is 20 years old male

通過上面的例子 其中student是元祖名稱,‘name age sex’是元組總元素名稱,用空格隔開,我們訪問元祖物件中的元素時可以使用逗號操作符(.)讀取物件中某個感興趣的元素,而不必像原始元組中,需要記錄下標代表的元素含義。

下面瞭解一下rename引數的作用:

    import collections
    with_class=collections.namedtuple('Person','name age class gender',rename=True)
    print with_class._fields
    two_ages=collections.namedtuple('Person','name age gender age',rename=True)

    print two_ages._fields其輸出結果為:
    ('name', 'age', '_2', 'gender')
    ('name', 'age', 'gender', '_3')

我們使用rename=True的方式開啟重新命名選項。
可以看到第一個集合中的class被重新命名為 ‘2′ ; 第二個集合中重複的age被重新命名為 ‘_3′;這是因為namedtuple在重新命名的時候使用了下劃線 加元素所在索引數的方式進行重新命名。

2、namedtuple高階用法

我們通過定義一個命名元組,如下:
Point = namedtuple(‘Point’, [‘x’, ‘y’], verbose=True)
那麼Point是一個元祖的子類,類結構具體如下:

class Point(tuple):
    'Point(x, y)'

    __slots__ = ()

    _fields = ('x', 'y')

    def __new__(_cls, x, y):
        'Create new instance of Point(x, y)'
        return _tuple.__new__(_cls, (x, y))

    @classmethod
    def _make(cls, iterable, new=tuple.__new__, len=len):
        'Make a new Point 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 'Point(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))

    def _replace(_self, **kwds):
        'Return a new Point 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' % kwds.keys())
        return result

    def __getnewargs__(self):
        'Return self as a plain tuple.  Used by copy and pickle.'
        return tuple(self)

    __dict__ = _property(_asdict)

    def __getstate__(self):
        'Exclude the OrderedDict from pickling'
        pass

    x = _property(_itemgetter(0), doc='Alias for field number 0')

    y = _property(_itemgetter(1), doc='Alias for field number 1')