Python collections系列之可命名元组
可命名元组(namedtuple)
根据nametuple可以创建一个包含tuple所有功能以及其他功能的类
1、创建一个坐标类
import collections
# 创建类, defaultdict,坐标中会使用
MytupleClass = collections.namedtuple('MytupleClass',['x', 'y', 'z']) obj = MytupleClass(11, 22, 33)
2、查询类中的x,y,z坐标
# 默认情况下元组只能使用索引进行访问,通过创建坐标类后,可以使用x,y,z进行访问 print(obj.x)
print(obj.y)
print(obj.z) 输出结果:
11
22
33
3、可命名元组的方法
# 获取MytupleClass的方法 print(help(MytupleClass))
class Mytuple(__builtin__.tuple)
| Mytuple(x, y)
|
| Method resolution order:
| Mytuple
| __builtin__.tuple
| __builtin__.object
|
| Methods defined here:
|
| __getnewargs__(self)
| Return self as a plain tuple. Used by copy and pickle.
|
| __getstate__(self)
| Exclude the OrderedDict from pickling
|
| __repr__(self)
| Return a nicely formatted representation string
|
| _asdict(self)
| Return a new OrderedDict which maps field names to their values
|
| _replace(_self, **kwds)
| Return a new Mytuple object replacing specified fields with new values
|
| ----------------------------------------------------------------------
| Class methods defined here:
|
| _make(cls, iterable, new=<built-in method __new__ of type object>, len=<built-in function len>) from __builtin__.type
| Make a new Mytuple object from a sequence or iterable
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| __new__(_cls, x, y)
| Create new instance of Mytuple(x, y)
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| Return a new OrderedDict which maps field names to their values
|
| x
| Alias for field number 0
|
| y
| Alias for field number 1
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| _fields = ('x', 'y')
|
| ----------------------------------------------------------------------
| Methods inherited from __builtin__.tuple:
|
| __add__(...)
| x.__add__(y) <==> x+y
|
| __contains__(...)
| x.__contains__(y) <==> y in x
|
| __eq__(...)
| x.__eq__(y) <==> x==y
|
| __ge__(...)
| x.__ge__(y) <==> x>=y
|
| __getattribute__(...)
| x.__getattribute__('name') <==> x.name
|
| __getitem__(...)
| x.__getitem__(y) <==> x[y]
|
| __getslice__(...)
| x.__getslice__(i, j) <==> x[i:j]
|
| Use of negative indices is not supported.
|
| __gt__(...)
| x.__gt__(y) <==> x>y
|
| __hash__(...)
| x.__hash__() <==> hash(x)
|
| __iter__(...)
| x.__iter__() <==> iter(x)
|
| __le__(...)
| x.__le__(y) <==> x<=y
|
| __len__(...)
| x.__len__() <==> len(x)
|
| __lt__(...)
| x.__lt__(y) <==> x<y
|
| __mul__(...)
| x.__mul__(n) <==> x*n
|
| __ne__(...)
| x.__ne__(y) <==> x!=y
|
| __rmul__(...)
| x.__rmul__(n) <==> n*x
|
| __sizeof__(...)
| T.__sizeof__() -- size of T in memory, in bytes
|
| count(...)
| T.count(value) -> integer -- return number of occurrences of value
|
| index(...)
| T.index(value, [start, [stop]]) -> integer -- return first index of value.
| Raises ValueError if the value is not present.
Mytuple
Python collections系列之可命名元组的更多相关文章
- Python collections系列之有序字典
有序字典(orderedDict ) orderdDict是对字典类型的补充,他记住了字典元素添加的顺序 1.创建一个有序字典 import collections dic = collections ...
- Python collections系列之计数器
计数器(counter) Counter是对字典(无序)类型的补充,用于追踪值的出现次数. 使用counter需要导入 collections 类 ps:具备字典的所有功能 + 自己的功能 1.创建一 ...
- Python collections系列之单向队列
单向队列(deque) 单项队列(先进先出 FIFO ) 1.创建单向队列 import queue q = queue.Queue() q.put(') q.put('evescn') 2.查看单向 ...
- Python collections系列之双向队列
双向队列(deque) 一个线程安全的双向队列 1.创建一个双向队列 import collections d = collections.deque() d.append(') d.appendle ...
- Python collections系列之默认字典
默认字典(defaultdict) defaultdict是对字典的类型的补充,它默认给字典的值设置了一个类型. 1.创建默认字典 import collections dic = collecti ...
- python collection系列
collection系列 不常用功能,需要进行模块功能导入: import collection Counter 常用方法测试: #!/usr/local/env python3 ''' Author ...
- Python 第三篇(下):collections系列、集合(set)、单双队列、深浅copy、内置函数
一.collections系列: collections其实是python的标准库,也就是python的一个内置模块,因此使用之前导入一下collections模块即可,collections在py ...
- python递归、collections系列以及文件操作进阶
global log 127.0.0.1 local2 daemon maxconn log 127.0.0.1 local2 info defaults log global mode http t ...
- python基础知识4——collection类——计数器,有序字典,默认字典,可命名元组,双向队列
1.计数器(counter) Counter是对字典类型的补充,用于追踪值的出现次数. ps:具备字典的所有功能 + 自己的功能 Counter 我们从中挑选一些相对常用的方法来举例: 在上面的例子 ...
随机推荐
- gst-rtsp-server编译测试
最近在做dm368的开发,打算在368上移植个gst-rtsp-server.先在电脑上折腾了一天,终于是可以运行了. 我的虚拟机上早先已经安装了gstreamer-0.10(gstreamer版本太 ...
- Android驱动开发全过程(有图有真相)
转:https://wenku.baidu.com/view/349bd159b7360b4c2e3f64b1.html
- imx6qsbd lvds dtc
lvds显示屏调试参考 1.基于飞思卡尔imxsolosabresd开发板Linux-3.10.53 lvds屏幕调试: http://blog.csdn.net/qq_37375427/articl ...
- [SCOI2003]蜘蛛难题
题目 对于当年来说似乎是神题?? 做法 对于联通注水来说,我们考虑把所有能平分到水的桶同时加高度,然后暴力判断 My complete code copy来的代码 #include <cstdi ...
- javax.servlet.jsp.JspException cannot be resolved to a type 和 javax.servlet.jsp.PageContext cannot be resolved to a type 解决办法
今天我从码云上拉一个项目下来,是个maven项目,闲来无事自己研究研究,发现刚拉下来,项目就有报错,我一看是httpServletRequest cannot be resolved to a typ ...
- 剑指Offer——反转链表
Question 输入一个链表,反转链表后,输出链表的所有元素. Solution 如果空间复杂度要求为O(1)的话,可以考虑用三个指针来进行反转 如果没有空间复杂度限制的话,可以考虑用一个栈,将节点 ...
- 智能穿戴设备移动APP端与外设数据传输协议
S1 Communication Layer specifications 1. Purpose of This Document ...
- VC++异常捕获??
1. std: #include <string> #include<iostream> // for cerr //#include <stdexcept> // ...
- 缓存淘汰算法--LRU算法(转)
(转自:http://flychao88.iteye.com/blog/1977653) 1. LRU1.1. 原理 LRU(Least recently used,最近最少使用)算法根据数据的历史访 ...
- Linux(CentOS)下.NET,mono环境的安装步骤整理
本文Linux使用的是CentOS 7 1.软件下载 CentOS:https://www.centos.org/download/ mono-4.2.1.36.tar.bz2 http://down ...