您的位置:首页 > 编程语言 > Python开发

Python-api: collections——High-performance container datatypes

2017-08-23 10:18 363 查看

Python-api: collections——High-performance container datatypes

Python-api collectionsHigh-performance container datatypes
namedtuple

来源:https://docs.python.org/2/library/collections.html

一点一点更新,用到一点更新一点

namedtuple

namedtuple主要是为了增加tuple的可读性,它可以用于一系列比较规整的tuple,并且namedtuple可以通过名称去索引而tuple只能通过下标去索引

Person = namedtuple('Person', 'name age gender')
print(type(Person))
Bob = Person(name = 'Bob', age = 29, gender = 'female')
print(Bob.name)
print(Bob)


namedtuple默认是不能用关键字的,如果出现类似class,def等关键字会报错,可以通过rename参数来解决这个问题:

#rename = true 可以考虑到Python关键字
Person = namedtuple('Person', 'name class gender', rename = true)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: