python 2-2 如何为元组中的每个元素命名, 提高程序可读性-collections.namedtuple
2017-01-14 10:24
579 查看
解决方案:
1.使用下标 定义类似其语言的枚举类型,也就是定义一系列的数值常量
NAME,AGE,SEX,EMAIL=xrange(4)
student=(‘jim’,16,’male’,’weihuap8@126.com’)
print student[EMAIL]
2.使用关键字namedtuple 替代内置的tuple
方案1:
方案2:
1.使用下标 定义类似其语言的枚举类型,也就是定义一系列的数值常量
NAME,AGE,SEX,EMAIL=xrange(4)
student=(‘jim’,16,’male’,’weihuap8@126.com’)
print student[EMAIL]
2.使用关键字namedtuple 替代内置的tuple
from collections import namedtuple Person = namedtuple('Person',['name','age','sex','email']) person1 = Person("xiaowang",55,1,'xiaowang@126.com') print person1.email import collections dir(collections) ['Callable', 'Container', 'Counter', 'Hashable', 'ItemsView', 'Iterable', 'Iterator', 'KeysView', 'Mapping', 'MappingView', 'MutableMapping', 'MutableSequence', 'MutableSet', 'OrderedDict', 'Sequence', 'Set', 'Sized', 'ValuesView', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_abcoll', '_chain', '_get_ident', '_heapq', '_iskeyword', '_itemgetter', '_repeat', '_starmap', '_sys', 'defaultdict', 'deque', 'namedtuple'] >>> >>> help(collections.namedtuple) Help on function namedtuple in module collections: namedtuple(typename, field_names, verbose=False, rename=False) Returns a new subclass of tuple with named fields. >>> Point = namedtuple('Point', 'x y') >>> Point.__doc__ # docstring for the new class 'Point(x, y)' >>> p = Point(11, y=22) # instantiate with positional args or keywords >>> p[0] + p[1] # indexable like a plain tuple 33 >>> x, y = p # unpack like a regular tuple >>> x, y (11, 22) >>> p.x + p.y # fields also accessable by name 33 >>> d = p._asdict() # convert to a dictionary >>> d['x'] 11 >>> Point(**d) # convert from a dictionary Point(x=11, y=22) >>> p._replace(x=100) # _replace() is like str.replace() but targets named fields Point(x=100, y=22) def namedtuple(typename, field_names, verbose=False, rename=False): """Returns a new subclass of tuple with named fields. >>> Point = namedtuple('Point', ['x', 'y']) >>> Point.__doc__ # docstring for the new class 'Point(x, y)' >>> p = Point(11, y=22) # instantiate with positional args or keywords >>> p[0] + p[1] # indexable like a plain tuple 33 >>> x, y = p # unpack like a regular tuple >>> x, y (11, 22) >>> p.x + p.y # fields also accessable by name 33 >>> d = p._asdict() # convert to a dictionary >>> d['x'] 11 >>> Point(**d) # convert from a dictionary Point(x=11, y=22) >>> p._replace(x=100) # _replace() is like str.replace() but targets named fields Point(x=100, y=22)
方案1:
t = range(4) t2 = xrange(4) NAME,AGE,SEX,EMAIL = xrange(4) print NAME,AGE,SEX,EMAIL person = ('weihuap',23,1,'xxyy@126.com') print person[NAME]
方案2:
from collections import namedtuple Person = namedtuple('Person',['name','age','sex','email']) person1 = Person("xiaowang",55,1,'xiaowang@126.com') print person1.email person2 = person1._replace(email="xiaoming333@126.com") print person1.email print person2.email dperson = person1._asdict() print dperson['email'] dict1 = {'age':185,'email':'xiaoming@126.com','sex':2,'name':'xiaoming'} tuple1 = Person(**dict1) print tuple1.name
相关文章推荐
- python高效编程技巧2(如何为元组的每个元素命名,提高程序的可读性)
- python-2-如何为元组中的每个元素命名,提高程序可读性?
- 如何在元组中的每个元素命名,提高程序可读性
- Python高级编程-如何为元祖中的每个元素命名,提高程序可读性?
- python 为元组中的每个元素命名,提高程序可读性
- 2-2为元组中的每个元素命名,提高程序可读性
- 为元组中的每个元素命名,提高程序可读性
- 给元组的每个元素命名,提高程序可读性
- 如何为元祖中的每个元素命名以提高可读性
- 为tuple中的元素命名,提高程序的可读性
- python_如何为元组中每个元素命名
- Python高效编程-为元组的每个元素命名
- python基础学习之如何对元组各个元素进行命名详解
- python的list由多个tuple组成,如何返回每个tuple的每个元素
- python【2】为元祖中的元素命名,提高可读性
- 自定义命名的元组collections.namedtuple
- 命名元组 collections.namedtuple
- python 为元组的每个元素命名
- <24>python学习笔记——为元组中的每个元素命名
- TypeError: can only concatenate tuple (not "str") to tuple(python 元组只有一个元素需要加逗号)